SELECT 
  cscart_banners.banner_id, 
  cscart_banners.type, 
  cscart_banners.target, 
  cscart_banners.status, 
  cscart_banners.timestamp, 
  cscart_banner_descriptions.banner, 
  cscart_banner_descriptions.description, 
  cscart_banner_descriptions.url, 
  cscart_banner_images.banner_image_id, 
  cscart_banner_descriptions.et_text, 
  cscart_banner_descriptions.et_settings, 
  cscart_banners.company_id 
FROM 
  cscart_banners 
  LEFT JOIN cscart_banner_descriptions ON cscart_banner_descriptions.banner_id = cscart_banners.banner_id 
  AND cscart_banner_descriptions.lang_code = 'en' 
  LEFT JOIN cscart_banner_images ON cscart_banner_images.banner_id = cscart_banners.banner_id 
  AND cscart_banner_images.lang_code = 'en' 
WHERE 
  1 
  AND (
    cscart_banners.type != 'G' 
    OR cscart_banner_images.banner_image_id IS NOT NULL
  ) 
  AND cscart_banners.status = 'A' 
  AND IF (
    cscart_banners.type not like 'abt__ut2', 
    'available', 
    IF (
      cscart_banners.type like 'abt__ut2' 
      AND (
        cscart_banners.abt__ut2_use_avail_period = 'N' 
        OR (
          cscart_banners.abt__ut2_use_avail_period = 'Y' 
          AND (
            cscart_banners.abt__ut2_avail_from = 0 
            OR cscart_banners.abt__ut2_avail_from <= 1732284482
          ) 
          AND (
            cscart_banners.abt__ut2_avail_till = 0 
            OR cscart_banners.abt__ut2_avail_till >= 1732284482
          )
        )
      ), 
      'available', 
      'not available'
    )
  ) = 'available' 
  AND cscart_banners.company_id = '25' 
ORDER BY 
  cscart_banner_descriptions.banner asc

Query time 0.00172

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "4.60"
    },
    "ordering_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "1.00"
      },
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_banners",
            "access_type": "ref",
            "possible_keys": [
              "status",
              "company_id"
            ],
            "key": "company_id",
            "used_key_parts": [
              "company_id"
            ],
            "key_length": "4",
            "ref": [
              "const"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 1,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "1.00",
              "eval_cost": "0.20",
              "prefix_cost": "1.20",
              "data_read_per_join": "2K"
            },
            "used_columns": [
              "banner_id",
              "status",
              "type",
              "target",
              "timestamp",
              "abt__ut2_use_avail_period",
              "abt__ut2_avail_from",
              "abt__ut2_avail_till",
              "company_id"
            ],
            "attached_condition": "((`webmarco`.`cscart_banners`.`status` = 'A') and (if((not((`webmarco`.`cscart_banners`.`type` like 'abt__ut2'))),'available',if(((`webmarco`.`cscart_banners`.`type` like 'abt__ut2') and ((`webmarco`.`cscart_banners`.`abt__ut2_use_avail_period` = 'N') or ((`webmarco`.`cscart_banners`.`abt__ut2_use_avail_period` = 'Y') and ((`webmarco`.`cscart_banners`.`abt__ut2_avail_from` = 0) or (`webmarco`.`cscart_banners`.`abt__ut2_avail_from` <= 1732284482)) and ((`webmarco`.`cscart_banners`.`abt__ut2_avail_till` = 0) or (`webmarco`.`cscart_banners`.`abt__ut2_avail_till` >= 1732284482))))),'available','not available')) = 'available'))"
          }
        },
        {
          "table": {
            "table_name": "cscart_banner_descriptions",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "banner_id",
              "lang_code"
            ],
            "key_length": "9",
            "ref": [
              "webmarco.cscart_banners.banner_id",
              "const"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 1,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "1.00",
              "eval_cost": "0.20",
              "prefix_cost": "2.40",
              "data_read_per_join": "5K"
            },
            "used_columns": [
              "banner_id",
              "banner",
              "url",
              "description",
              "lang_code",
              "et_text",
              "et_settings"
            ]
          }
        },
        {
          "table": {
            "table_name": "cscart_banner_images",
            "access_type": "eq_ref",
            "possible_keys": [
              "banner"
            ],
            "key": "banner",
            "used_key_parts": [
              "banner_id",
              "lang_code"
            ],
            "key_length": "9",
            "ref": [
              "webmarco.cscart_banners.banner_id",
              "const"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 1,
            "filtered": "100.00",
            "using_index": true,
            "cost_info": {
              "read_cost": "1.00",
              "eval_cost": "0.20",
              "prefix_cost": "3.60",
              "data_read_per_join": "16"
            },
            "used_columns": [
              "banner_image_id",
              "banner_id",
              "lang_code"
            ],
            "attached_condition": "<if>(found_match(cscart_banner_images), ((`webmarco`.`cscart_banners`.`type` <> 'G') or (`webmarco`.`cscart_banner_images`.`banner_image_id` is not null)), true)"
          }
        }
      ]
    }
  }
}