MySQL按顺序排序,大大减慢查询速度

时间:2015-06-26 12:40:25

标签: mysql sql query-optimization

我有以下查询,尽管它的复杂性,但在以下形状中表现良好:

SELECT 

`listings`.*, 
`auth_agents`.*, 
`symbols`.`code` AS `symbol_code`,
`symbols`.`text` AS `symbol_text`,
`symbols`.`position` AS `symbol_position`,
GROUP_CONCAT(DISTINCT IFNULL(`categories_countries`.`translation`, `c`.`name`) ORDER BY CASE WHEN `c`.`category_id` = 106 THEN 1 ELSE `c`.`category_id` END) as `category_names`,
`pv_1`.`value` AS `transaction_type_name`,
`pv_2`.`value` AS `property_type_name`,
`listings_statuses`.`name` AS `status`,
CONCAT(`pv_3`.`value`, 'property/', `listings`.`slug`, '/', `listings`.`listing_id`) AS `url`,
price_range(`listings`.`price_sales_min`, `listings`.`price_sales`, `symbols`.`code`, `symbols`.`position`, `listings`.`listings_price_qualifier_id`) AS `price_sales_range`,
price_range(`listings`.`price_lettings_min`, `listings`.`price_lettings`, `symbols`.`code`, `symbols`.`position`, `listings`.`listings_price_qualifier_id`) AS `price_lettings_range`,
summary(`listings`.`description`, `listings`.`summary`) AS `summary`,
short_title(`categories`.`name`, `categories`.`title`, `pv_1`.`value`, `listings`.`city`, `listings`.`region`, `listings`.`area`) AS `short_title`,
IF(`listings`.`listings_price_qualifier_id` = 2, TRUE, FALSE) AS `POA`,
IF((SELECT `listings_categories`.`listing_id` FROM `listings_categories` WHERE `listings_categories`.`listing_id` = `listings`.`listing_id` AND `listings_categories`.`category_id` = 124 LIMIT 1) IS NOT NULL, TRUE, FALSE) AS `serviced_office`,

GROUP_CONCAT(DISTINCT CONCAT_WS('|', `auth_agents_staff`.`auth_agents_staff_id`, `auth_staff`.`f_name`, `auth_staff`.`l_name`, `auth_staff`.`photo`)) AS `agents_staff`,
(SELECT CONCAT('http://cdn.commercialpeople.com/images/', `listings`.`auth_agent_id`, '/', `listings_media`.`filename`) FROM `listings_media` WHERE `listings_media`.`listing_id` = `listings`.`listing_id` ORDER BY `default` DESC LIMIT 1) AS `image`,
(SELECT COUNT(`listings_media`.`listings_media_id`) FROM `listings_media` WHERE `listings_media`.`listing_id` = `listings`.`listing_id` AND `listings_media`.`listings_media_type_id` = 1) AS `image_count`,
(SELECT COUNT(`listings_media`.`listings_media_id`) FROM `listings_media` WHERE `listings_media`.`listing_id` = `listings`.`listing_id` AND `listings_media`.`listings_media_type_id` = 3) AS `pdf_count`,
(SELECT COUNT(`listings_media`.`listings_media_id`) FROM `listings_media` WHERE `listings_media`.`listing_id` = `listings`.`listing_id` AND `listings_media`.`listings_media_type_id` = 4) AS `floor_plan_count`,


distance(`listings`.`lat`, `listings`.`lng`, 51.5073500, -0.1277600) AS `distance` 

FROM `listings` 
JOIN `listings_coords` ON `listings_coords`.`listing_id` = `listings`.`listing_id` 

JOIN `auth_agents` ON `listings`.`auth_agent_id` = `auth_agents`.`auth_agent_id` 
JOIN `branches` USING (`branch_id`) 
LEFT JOIN `listings_auth_agents_staff` ON `listings_auth_agents_staff`.`listing_id` = `listings`.`listing_id` 
LEFT JOIN `auth_agents_staff` ON `listings_auth_agents_staff`.`listing_id` IS NOT NULL AND `listings_auth_agents_staff`.`auth_agents_staff_id` = `auth_agents_staff`.`auth_agents_staff_id` 
LEFT JOIN `auth` AS `auth_staff` ON `listings_auth_agents_staff`.`listing_id` IS NOT NULL AND `auth_agents_staff`.`auth_agents_staff_id` IS NOT NULL AND 
`auth_staff`.`auth_id` = `auth_agents_staff`.`auth_id` 

JOIN `parameters_values` AS `pv_1` 
ON `pv_1`.`parameter_id` = 2 AND `pv_1`.`country_id` = 223 AND `pv_1`.`key` = `listings`.`transaction_type` 

JOIN `parameters_values` AS `pv_2` 
ON `pv_2`.`parameter_id` = 1 AND `pv_2`.`country_id` = 223 AND `pv_2`.`key` = `listings`.`property_type` 

JOIN `parameters_values` AS `pv_3` 
ON `pv_3`.`parameter_id` = 5 AND `pv_2`.`country_id` = 223 

JOIN `countries` ON `listings`.`country_id` = `countries`.`country_id` 
JOIN `countries_symbols` ON `countries`.`country_id` = `countries_symbols`.`country_id` 
JOIN `symbols` ON `countries_symbols`.`symbol_id` = `symbols`.`symbol_id` 

JOIN `listings_units` ON `listings`.`listings_unit_id` = `listings_units`.`listings_unit_id` 

JOIN `listings_units` AS `user_units` ON `user_units`.`listings_unit_id` = 2 


JOIN `listings_statuses` ON `listings`.`listings_status_id` = `listings_statuses`.`listings_status_id` 
LEFT JOIN `listings_statuses_countries` ON `listings_statuses`.`listings_status_id` = `listings_statuses_countries`.`listings_status_id` 
AND `listings_statuses_countries`.`country_id` = '223' 

JOIN `listings_categories` ON `listings_categories`.`listing_id` = `listings`.`listing_id`  
JOIN `categories` ON `categories`.`category_id` = `listings_categories`.`category_id` 

JOIN `listings_categories` AS `lc` ON `lc`.`listing_id` = `listings`.`listing_id`  
JOIN `categories` AS `c` ON `c`.`category_id` = `lc`.`category_id` 
LEFT JOIN `categories_countries` ON `c`.`category_id` = `categories_countries`.`category_id` AND `categories_countries`.`country_id` = '223' 

LEFT JOIN `listings_lettings`  
ON (`listings`.`transaction_type` = 2 OR `listings`.`transaction_type` = 3) AND `listings_lettings`.`listing_id` = `listings`.`listing_id` 
LEFT JOIN `listings_lettings_rent_frequencies`  
ON (`listings`.`transaction_type` = 2 OR `listings`.`transaction_type` = 3) AND `listings_lettings_rent_frequencies`.`listings_lettings_rent_frequency_id` = `listings_lettings`.`listings_lettings_rent_frequency_id` 

LEFT JOIN `listings_lettings_rent_frequencies` AS `llrf` ON `llrf`.`listings_lettings_rent_frequency_id` = 2 
WHERE `listings`.`active` = 1 AND `listings`.`published_flag` = 1 AND 
`listings`.`listings_status_id` = 1 AND
`listings`.`lat` BETWEEN 51.333436956522 AND 51.681263043478 AND 
`listings`.`lng` BETWEEN -0.40717687879372 AND 0.15165687879372 AND 
st_within(`listings_coords`.`coords`, @poly) AND 

IF (`listings`.`transaction_type` = 2 || `listings`.`transaction_type` = 3, 
`listings`.`price_lettings` * `listings_lettings_rent_frequencies`.`months` >= `llrf`.`months` * 500,
TRUE
) AND 
IF(`listings_units`.`meters` IS NULL, TRUE, `listings`.`max_size` * `listings_units`.`meters` >= 100 * `user_units`.`meters`) AND 
`listings`.`country_id` = 223 

GROUP BY `listings`.`listing_id`  
ORDER BY `listings`.`created_at` 
LIMIT 10; 

如您所见,第一个查询抓取多边形,然后通过空间函数在多边形搜索中使用该多边形。查询在aprox 0.06sec。

中执行

但是,添加任何ORDER BY子句会导致性能急剧下降,最终会在17秒内执行。

现在 - 我已经完成了一些研究,我知道这可能与聚簇索引有关,或者只是添加正确的索引,但是花了几个小时才尝试返回ID&和然后运行另一个查询来获取数据或者只是删除GROUP BY并删除连接(这似乎最初有效,但当我加入几个表进行价格搜索时出现了另一个性能问题) - 我放弃了

我试图在没有分组的情况下运行查询,然后对结果进行分组,例如:

SELECT * FROM ( *the query* ) AS results GROUP BY listing_id

但我得到了相同的结果。

有没有办法优化查询,所以我可以进行分组和排序?

这是EXPLAIN:

    |  1 | PRIMARY            | countries                          | const  | PRIMARY                                                                           | PRIMARY                              | 4       | const                                                            |    1 | Using index; Using temporary; Using filesort                              |
    |  1 | PRIMARY            | user_units                         | const  | PRIMARY                                                                           | PRIMARY                              | 4       | const                                                            |    1 | NULL                                                                      |
    |  1 | PRIMARY            | listings_statuses                  | const  | PRIMARY                                                                           | PRIMARY                              | 4       | const                                                            |    1 | NULL                                                                      |
    |  1 | PRIMARY            | listings_statuses_countries        | const  | PRIMARY,country_id                                                                | PRIMARY                              | 8       | const,const                                                      |    1 | Using index                                                               |
    |  1 | PRIMARY            | countries_symbols                  | ref    | PRIMARY,symbol_id                                                                 | PRIMARY                              | 4       | const                                                            |    1 | Using index                                                               |
    |  1 | PRIMARY            | symbols                            | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.countries_symbols.symbol_id                           |    1 | NULL                                                                      |
    |  1 | PRIMARY            | pv_2                               | ref    | parameter_id,country_id                                                           | parameter_id                         | 8       | const,const                                                      |    2 | NULL                                                                      |
    |  1 | PRIMARY            | pv_1                               | ref    | parameter_id,country_id                                                           | parameter_id                         | 8       | const,const                                                      |    3 | NULL                                                                      |
    |  1 | PRIMARY            | pv_3                               | ref    | parameter_id                                                                      | parameter_id                         | 4       | const                                                            |    4 | NULL                                                                      |
    |  1 | PRIMARY            | listings                           | range  | SEE BELOW                                                                         | compound                             | 20      | NULL                                                             | 2594 | Using index condition; Using where; Using join buffer (Block Nested Loop) |
    |  1 | PRIMARY            | listings_units                     | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.listings.listings_unit_id                             |    1 | Using where                                                               |
    |  1 | PRIMARY            | listings_coords                    | eq_ref | PRIMARY,coords                                                                    | PRIMARY                              | 4       | cp_staging.listings.listing_id                                   |    1 | Using where                                                               |
    |  1 | PRIMARY            | auth_agents                        | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.listings.auth_agent_id                                |    1 | NULL                                                                      |
    |  1 | PRIMARY            | branches                           | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.listings.branch_id                                    |    1 | Using index                                                               |
    |  1 | PRIMARY            | listings_categories                | ref    | PRIMARY,listings_categories_category_id_foreign                                   | PRIMARY                              | 4       | cp_staging.listings.listing_id                                   |    1 | Using index                                                               |
    |  1 | PRIMARY            | lc                                 | ref    | PRIMARY,listings_categories_category_id_foreign                                   | PRIMARY                              | 4       | cp_staging.listings.listing_id                                   |    1 | Using index                                                               |
    |  1 | PRIMARY            | listings_auth_agents_staff         | ref    | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.listings.listing_id                                   |    1 | Using index                                                               |
    |  1 | PRIMARY            | categories                         | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.listings_categories.category_id                       |    1 | NULL                                                                      |
    |  1 | PRIMARY            | c                                  | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.lc.category_id                                        |    1 | NULL                                                                      |
    |  1 | PRIMARY            | auth_agents_staff                  | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.listings_auth_agents_staff.auth_agents_staff_id       |    1 | Using where                                                               |
    |  1 | PRIMARY            | auth_staff                         | eq_ref | PRIMARY                                                                           | PRIMARY                              | 4       | cp_staging.auth_agents_staff.auth_id                             |    1 | Using where                                                               |
    |  1 | PRIMARY            | categories_countries               | eq_ref | PRIMARY,categories_countries_country_id_foreign                                   | PRIMARY                              | 8       | cp_staging.lc.category_id,const                                  |    1 | NULL                                                                      |
    |  1 | PRIMARY            | listings_lettings                  | ref    | listings_lettings_listing_id_foreign                                              | listings_lettings_listing_id_foreign | 4       | cp_staging.listings.listing_id                                   |    1 | Using where                                                               |
    |  1 | PRIMARY            | listings_lettings_rent_frequencies | eq_ref | PRIMARY,listings_lettings_rent_frequencies_id_months                              | PRIMARY                              | 4       | cp_staging.listings_lettings.listings_lettings_rent_frequency_id |    1 | Using where                                                               |
    |  1 | PRIMARY            | llrf                               | const  | PRIMARY,listings_lettings_rent_frequencies_id_months                              | PRIMARY                              | 4       | const                                                            |    1 | Using where                                                               |
    |  5 | DEPENDENT SUBQUERY | listings_media                     | ref    | listings_media_listing_id_foreign,listings_media_listings_media_type_id_foreign   | listings_media_listing_id_foreign    | 4       | func                                                             |    1 | Using where                                                               |
    |  4 | DEPENDENT SUBQUERY | listings_media                     | ref    | listings_media_listing_id_foreign,listings_media_listings_media_type_id_foreign   | listings_media_listing_id_foreign    | 4       | func                                                             |    1 | Using where                                                               |
    |  3 | DEPENDENT SUBQUERY | listings_media                     | ref    | listings_media_listing_id_foreign,listings_media_listings_media_type_id_foreign   | listings_media_listing_id_foreign    | 4       | func                                                             |    1 | Using where                                                               |
    |  2 | DEPENDENT SUBQUERY | listings_categories                | eq_ref | PRIMARY,listings_categories_category_id_foreign                                   | PRIMARY                              | 8       | func,const                                                       |    1 | Using index                                                               |

listings表的第5列值(已拉出以便可以保留上表的格式)PRIMARY,listings_former_id_unique,listings_price_sales_index,listings_price_sales_min_index,listings_price_lettings_index,listings_price_lettings_min_index,listings_country_id_foreign,listings_agent_id_foreign,listings_listings_status_id_foreign,listings_listings_price_qualifier_id_foreign,listings_listings_unit_id_foreign,listings_listings_tenure_type_id_foreign,agents_staff_id,listings_listings_class_id_foreign_idx,listings_listings_location_type_id_foreign_idx,listings_listings_branch_id_foreign_idx,latlng,listings_city,listings_created_at,listings_country_created_at,listings_transaction_type,listings_listing_id_transaction_type,listings_listings_coords_id,listings_created_at_listing_id,compound

2 个答案:

答案 0 :(得分:0)

  1. 删除除socket.socketlistingslistings_lettings_rent_frequenciesllrf以外的所有表格。
  2. 让我们优化该查询。保持GROUP BY,ORDER BY和LIMIT以及仅部分WHERE。
  3. 看看是否能为您提供所需的10 listings_units
  4. 如果我们可以让该查询合理地快速运行,那么请执行

    listing_ids

答案 1 :(得分:0)

我遗漏了所有可选位(也删除了GROUP BY,因为这是不必要的),它几乎归结为:

SET @poly = (SELECT `polygon` FROM locations WHERE location_id = 65);

SELECT

`listings`.`listing_id`

FROM `listings`
JOIN `listings_coords` ON `listings_coords`.`listing_id` = `listings`.`listing_id`
JOIN `listings_units` ON `listings`.`listings_unit_id` = `listings_units`.`listings_unit_id`
JOIN `listings_units` AS `user_units` ON `user_units`.`listings_unit_id` = 2

LEFT JOIN `listings_lettings`
ON (`listings`.`transaction_type` = 2 OR `listings`.`transaction_type` = 3) AND `listings_lettings`.`listing_id` = `listings`.`listing_id`
LEFT JOIN `listings_lettings_rent_frequencies`
ON (`listings`.`transaction_type` = 2 OR `listings`.`transaction_type` = 3) AND `listings_lettings_rent_frequencies`.`listings_lettings_rent_frequency_id` = `listings_lettings`.`listings_lettings_rent_frequency_id`

LEFT JOIN `listings_lettings_rent_frequencies` AS `llrf` ON `llrf`.`listings_lettings_rent_frequency_id` = 2
WHERE
`listings`.`lat` BETWEEN 51.333436956522 AND 51.681263043478 AND
`listings`.`lng` BETWEEN -0.40717687879372 AND 0.15165687879372 AND
st_within(`listings_coords`.`coords`, @poly) AND

(
    `listings`.`transaction_type` = 1  OR
    `listings`.`price_lettings` * `listings_lettings_rent_frequencies`.`months` >= `llrf`.`months` * 500

)
AND
(
    `listings_units`.`meters` IS NULL OR
    `listings`.`max_size` * `listings_units`.`meters` >= 100 * `user_units`.`meters`
)
AND
`listings`.`country_id` = 223

ORDER BY `listings`.`created_at`
LIMIT 10;

现在似乎没有ORDER BY子句减慢速度。目前的解释:

+----+-------------+------------------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+----------------------------------------------------------+------+----------------------------------------------------+
| id | select_type | table                              | type   | possible_keys                                                                                                                                                                                                                         | key                                  | key_len | ref                                                      | rows | Extra                                              |
+----+-------------+------------------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+----------------------------------------------------------+------+----------------------------------------------------+
|  1 | SIMPLE      | user_units                         | const  | PRIMARY                                                                                                                                                                                                                               | PRIMARY                              | 4       | const                                                    |    1 | Using temporary; Using filesort                    |
|  1 | SIMPLE      | listings                           | range  | PRIMARY,listings_country_id_foreign,listings_listings_unit_id_foreign,latlng,listings_country_created_at,listings_country_units_created_at,listings_transaction_type,listings_listing_id_transaction_type,listings_lat_lng_created_at | latlng                               | 10      | NULL                                                     | 3728 | Using index condition; Using where                 |
|  1 | SIMPLE      | listings_coords                    | eq_ref | PRIMARY,coords                                                                                                                                                                                                                        | PRIMARY                              | 4       | cp.listings.listing_id                                   |    1 | Using where                                        |
|  1 | SIMPLE      | listings_lettings                  | ref    | listings_lettings_listing_id_foreign                                                                                                                                                                                                  | listings_lettings_listing_id_foreign | 4       | cp.listings.listing_id                                   |    1 | Using where                                        |
|  1 | SIMPLE      | listings_lettings_rent_frequencies | eq_ref | PRIMARY                                                                                                                                                                                                                               | PRIMARY                              | 4       | cp.listings_lettings.listings_lettings_rent_frequency_id |    1 | Using where                                        |
|  1 | SIMPLE      | listings_units                     | ALL    | PRIMARY                                                                                                                                                                                                                               | NULL                                 | NULL    | NULL                                                     |    6 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | llrf                               | const  | PRIMARY                                                                                                                                                                                                                               | PRIMARY                              | 4       | const                                                    |    1 | Using where                                        |
+----+-------------+------------------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+----------------------------------------------------------+------+----------------------------------------------------+

删除价格和尺寸条件会带来更好的性能,并且无需文件排序即可进行搜索。它可能与糟糕的索引有关,尽管正如所指出的那样 - 我确实有很多这样的索引。上述查询的当前性能是aprox 5s。同样删除st_within函数似乎解决了这个问题(0.047s)。正如你所说 - 我可以轻松地在子查询中执行初始搜索,获得所需的10个ID,并在外部查询中继续使用JOIN。

这是列表表的创建语句:

CREATE TABLE `listings` (
  `listing_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `auth_agent_id` int(10) unsigned NOT NULL,
  `country_id` int(10) unsigned NOT NULL,
  `branch_id` int(10) unsigned DEFAULT NULL,
  `property_type` tinyint(4) NOT NULL,
  `listings_status_id` int(10) unsigned NOT NULL,
  `listings_price_qualifier_id` int(10) unsigned NOT NULL,
  `listings_tenure_type_id` int(10) unsigned DEFAULT NULL,
  `listings_class_id` int(10) unsigned DEFAULT NULL,
  `listings_location_type_id` int(10) unsigned DEFAULT NULL,
  `transaction_type` enum('1','2','3') COLLATE utf8_bin NOT NULL,
  `auth_agents_staff_id` int(10) unsigned DEFAULT NULL,
  `published_flag` tinyint(1) NOT NULL,
  `address_1` char(255) COLLATE utf8_bin DEFAULT NULL,
  `address_2` char(255) COLLATE utf8_bin DEFAULT NULL,
  `area` char(150) COLLATE utf8_bin DEFAULT NULL,
  `city` char(255) COLLATE utf8_bin DEFAULT NULL,
  `region` char(255) COLLATE utf8_bin DEFAULT NULL,
  `postcode` char(255) COLLATE utf8_bin DEFAULT NULL,
  `hide_address` tinyint(1) NOT NULL DEFAULT '0',
  `street_view` tinyint(1) NOT NULL DEFAULT '1',
  `lat` decimal(10,6) NOT NULL,
  `lng` decimal(10,6) NOT NULL,
  `price_sales` decimal(14,2) DEFAULT NULL,
  `price_sales_min` decimal(10,2) DEFAULT NULL,
  `price_lettings` decimal(10,2) DEFAULT NULL,
  `price_lettings_min` decimal(10,2) DEFAULT NULL,
  `price_per_unit_sales` decimal(9,2) DEFAULT NULL,
  `price_per_unit_lettings` decimal(9,2) DEFAULT NULL,
  `min_size` decimal(10,2) DEFAULT NULL,
  `max_size` decimal(10,2) DEFAULT NULL,
  `listings_unit_id` int(10) unsigned NOT NULL,
  `summary` text COLLATE utf8_bin,
  `description` text COLLATE utf8_bin,
  `short_title` varchar(150) COLLATE utf8_bin DEFAULT NULL,
  `title` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `slug` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `agent_ref` char(255) COLLATE utf8_bin DEFAULT NULL,
  `active` tinyint(1) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `former_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`listing_id`),
  UNIQUE KEY `listings_transaction_listing_id` (`transaction_type`,`listing_id`),
  UNIQUE KEY `listings_former_id_unique` (`former_id`),
  KEY `listings_price_sales_index` (`price_sales`),
  KEY `listings_price_sales_min_index` (`price_sales_min`),
  KEY `listings_price_lettings_index` (`price_lettings`),
  KEY `listings_price_lettings_min_index` (`price_lettings_min`),
  KEY `listings_country_id_foreign` (`country_id`),
  KEY `listings_agent_id_foreign` (`auth_agent_id`),
  KEY `listings_listings_status_id_foreign` (`listings_status_id`),
  KEY `listings_listings_price_qualifier_id_foreign` (`listings_price_qualifier_id`),
  KEY `listings_listings_unit_id_foreign` (`listings_unit_id`),
  KEY `listings_listings_tenure_type_id_foreign` (`listings_tenure_type_id`),
  KEY `agents_staff_id` (`auth_agents_staff_id`),
  KEY `listings_listings_class_id_foreign_idx` (`listings_class_id`),
  KEY `listings_listings_location_type_id_foreign_idx` (`listings_location_type_id`),
  KEY `listings_listings_branch_id_foreign_idx` (`branch_id`),
  KEY `lat_lng_country_id_active_published` (`lat`,`lng`,`country_id`,`active`,`published_flag`),
  CONSTRAINT `listings_agent_id_foreign` FOREIGN KEY (`auth_agent_id`) REFERENCES `auth_agents` (`auth_agent_id`),
  CONSTRAINT `listings_country_id_foreign` FOREIGN KEY (`country_id`) REFERENCES `countries` (`country_id`),
  CONSTRAINT `listings_ibfk_1` FOREIGN KEY (`auth_agents_staff_id`) REFERENCES `agents_staff` (`agents_staff_id`) ON DELETE SET NULL ON UPDATE SET NULL,
  CONSTRAINT `listings_listings_branch_id_foreign` FOREIGN KEY (`branch_id`) REFERENCES `branches` (`branch_id`),
  CONSTRAINT `listings_listings_class_id_foreign` FOREIGN KEY (`listings_class_id`) REFERENCES `listings_classes` (`listings_class_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `listings_listings_location_type_id_foreign` FOREIGN KEY (`listings_location_type_id`) REFERENCES `listings_location_types` (`listings_location_type_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `listings_listings_price_qualifier_id_foreign` FOREIGN KEY (`listings_price_qualifier_id`) REFERENCES `listings_price_qualifiers` (`listings_price_qualifier_id`),
  CONSTRAINT `listings_listings_status_id_foreign` FOREIGN KEY (`listings_status_id`) REFERENCES `listings_statuses` (`listings_status_id`),
  CONSTRAINT `listings_listings_tenure_type_id_foreign` FOREIGN KEY (`listings_tenure_type_id`) REFERENCES `listings_tenure_types` (`listings_tenure_type_id`),
  CONSTRAINT `listings_listings_unit_id_foreign` FOREIGN KEY (`listings_unit_id`) REFERENCES `listings_units` (`listings_unit_id`)
) ENGINE=InnoDB AUTO_INCREMENT=34832 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;