繁体   English   中英

如何使以下查询在具有许多条件的查找中更有效

[英]how can i make the following query more efficient in lookup with many conditions

首先,我需要通过使用几个 where 条件限制以下查询查找中的结果来具有分页功能。

SELECT SQL_CALC_FOUND_ROWS
    a.uid, b.NAME
FROM
    `profiles` AS a FORCE INDEX(profiles_country_city_gender_index)
JOIN `users` AS b
ON b.id = a.uid
AND a.country = 'INDONESIA'
AND a.gender = 0
JOIN (
    SELECT
        a.uid
    FROM
       profile_details AS a
    JOIN profile_details AS kids ON kids.uid = a.uid
    AND kids.kids_pref = 1
    JOIN profile_details AS current ON current.uid = a.uid
    AND current.current_relationship = 1
    JOIN profile_details AS smoking ON smoking.uid = a.uid
    AND smoking.smoking_pref = 1
    ) AS e ON e.uid = a.uid
AND ( TIMESTAMPDIFF( YEAR, a.birth_date, NOW()) BETWEEN 25 AND 35 )
LIMIT 33;

这里的所有表都是与表用户的一对一关系,它们是

  • 简介
  • Profile_details

在用户中使用 id 列作为主键,在其他表中使用 uid 作为外键。 一开始,我对上述查询/设计没有问题,直到表增长到 300K 行,查询运行OK, Time: 0.726000s来获取结果,这对我来说太慢了。

我尝试使用 count(*) 根据上述条件计算行数并获得大致相同的结果,我需要有更快的方法从查找条件中获取行数,以使分页系统按预期工作等待的时间。

正如您在查询中看到的,我正在使用:

FORCE INDEX(profiles_country_city_gender_index)

我认为通过使用 scope 产生的较大行并没有太大帮助:

AND a.country = 'INDONESIA' 
AND a.gender = 0

结果(148801 行 scope 限制按国家和性别等于 0),如果我与城市配对,则查询时间不是问题,因为行结果要小得多,但当某天有更大的行时仍然会出现问题。

对于任何可能要求查询解释的人:

Explain SELECT SQL_CALC_FOUND_ROWS
        a.uid, 
        b.NAME ...

Results:

| select_type | table   | type   | possible_keys                      | key                                | key_len | ref              | rows   | filtered  | Extra                              |
+-------------+---------+--------+------------------------------------+------------------------------------+---------+------------------+--------+-----------+------------------------------------+
| SIMPLE      | a       | ref    | profiles_country_city_gender_index | profiles_country_city_gender_index | 242     | const            | 148801 | 10.00     | Using index condition; Using where |
| SIMPLE      | a       | ref    | profile_details_uid_foreign        | profile_details_uid_foreign        | 3       | restfulapi.a.uid | 1      | 100.00.00 | Using index                        |
| SIMPLE      | kids    | ref    | profile_details_uid_foreign        | profile_details_uid_foreign        | 3       | restfulapi.a.uid | 1      | 10.00     | Using where                        |
| SIMPLE      | current | ref    | profile_details_uid_foreign        | profile_details_uid_foreign        | 3       | restfulapi.a.uid | 1      | 10.00     | Using where                        |
| SIMPLE      | smoking | ref    | profile_details_uid_foreign        | profile_details_uid_foreign        | 3       | restfulapi.a.uid | 1      | 10.00     | Using where                        |
| SIMPLE      | b       | eq_ref | PRIMARY                            | PRIMARY                            | 3       | restfulapi.a.uid | 1      | 100.00.00 |                                    |

正如您在解释结果中看到的那样,没有表扫描或使用临时或使用范围,只有索引条件。 我想如果表格有至少 100 万行由国家 scope 返回,只需将缩放行的时间乘以 300K 就很糟糕了:(。

下面是表定义,以防有助于分析问题:

CREATE TABLE `profile_details` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `uid` mediumint(8) unsigned NOT NULL,
  `intents` tinyint(4) NOT NULL DEFAULT '3',
  `height` smallint(6) DEFAULT NULL,
  `body_type` tinyint(4) NOT NULL DEFAULT '5',
  `kids_pref` tinyint(4) NOT NULL DEFAULT '1',
  `drinking_pref` tinyint(4) NOT NULL DEFAULT '2',
  `living_with` tinyint(4) NOT NULL DEFAULT '0',
  `current_relationship` tinyint(4) NOT NULL DEFAULT '1',
  `sexual_pref` tinyint(4) NOT NULL DEFAULT '1',
  `smoking_pref` tinyint(4) NOT NULL DEFAULT '0',
  `status_online` tinyint(4) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `profile_details_uid_foreign` (`uid`),
  KEY `idx_multipart` (`intents`,`body_type`,`kids_pref`,`drinking_pref`,`living_with`,`current_relationship`,`sexual_pref`,`smoking_pref`),
  CONSTRAINT `profile_details_uid_foreign` FOREIGN KEY (`uid`) REFERENCES `users` (`id`)
)

CREATE TABLE `profiles` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `uid` mediumint(8) unsigned NOT NULL,
  `birth_date` date NOT NULL,
  `gender` tinyint(4) NOT NULL DEFAULT '0',
  `country` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'ID',
  `city` varchar(60) COLLATE utf8mb4_unicode_ci DEFAULT 'Makassar',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `latitude` double NOT NULL DEFAULT '0',
  `longitude` double NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `profiles_uid_foreign` (`uid`),
  KEY `profiles_birth_date_index` (`birth_date`),
  KEY `profiles_latitude_longitude_index` (`latitude`,`longitude`),
  KEY `profiles_country_city_gender_index` (`country`,`city`,`gender`),
  KEY `idx_country_gender_birthdate` (`country`,`gender`,`birth_date`),
  KEY `idx_country_city_gender_birthdate` (`country`,`city`,`gender`,`birth_date`),
  CONSTRAINT `profiles_uid_foreign` FOREIGN KEY (`uid`) REFERENCES `users` (`id`)
)

我怎样才能找到解决方案,我是否需要重新设计表格以获得理想的系统? 也许这是最后的选择。

编辑

我正在尝试您之前的建议,首先我在三列中添加了一个索引:

CREATE INDEX profiles_country_gender_birth_date_index on `profiles`(country,gender,birth_date);

我试图 select Count(*) without JOIN with profile_detail:

SELECT
    count(*)


FROM
    `profiles` AS a 
    FORCE INDEX ( profiles_country_gender_birth_date_index )
    JOIN `users` AS b ON b.id = a.uid 
and 
a.country = 'INDONESIA' 

    AND a.gender =1 
    AND a.birth_date BETWEEN NOW()- INTERVAL 35 YEAR 
    AND NOW()- INTERVAL 25 YEAR 

结果时间在 0.7 秒到 0.35 秒之间不稳定,我不知道为什么会这样。 以下是 Json 格式的解释查询计划,以防万一帮助找出罪魁祸首。

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "114747.38"
    },
    "nested_loop": [
      {
        "table": {
          "table_name": "a",
          "access_type": "range",
          "possible_keys": [
            "profiles_country_gender_birth_date_index"
          ],
          "key": "profiles_country_gender_birth_date_index",
          "used_key_parts": [
            "country",
            "gender",
            "birth_date"
          ],
          "key_length": "246",
          "rows_examined_per_scan": 94066,
          "rows_produced_per_join": 32961,
          "filtered": "100.00",
          "index_condition": "((`restfulapi`.`a`.`gender` = 1) and (`restfulapi`.`a`.`country` = 'INDONESIA') and (`restfulapi`.`a`.`birth_date` between <cache>((now() - interval 35 year)) and <cache>((now() - interval 25 year))))",
          "cost_info": {
            "read_cost": "15858.00",
            "eval_cost": "6592.23",
            "prefix_cost": "75194.00",
            "data_read_per_join": "16M"
          },
          "used_columns": [
            "uid",
            "birth_date",
            "gender",
            "country"
          ]
        }
      },
      {
        "table": {
          "table_name": "b",
          "access_type": "eq_ref",
          "possible_keys": [
            "PRIMARY"
          ],
          "key": "PRIMARY",
          "used_key_parts": [
            "id"
          ],
          "key_length": "3",
          "ref": [
            "restfulapi.a.uid"
          ],
          "rows_examined_per_scan": 1,
          "rows_produced_per_join": 32961,
          "filtered": "100.00",
          "using_index": true,
          "cost_info": {
            "read_cost": "32961.15",
            "eval_cost": "6592.23",
            "prefix_cost": "114747.38",
            "data_read_per_join": "89M"
          },
          "used_columns": [
            "id"
          ]
        }
      }
    ]
  }
}
INDEX(country, gender, birth_date)  -- in this order

并将birth_date的使用更改为“sargeable”:

AND  ( TIMESTAMPDIFF( YEAR, a.birth_date, NOW()) BETWEEN 25 AND 35 )

AND a.birth_date BETWEEN NOW() - INTERVAL 35 YEAR
                     AND NOW() - INTERVAL 25 YEAR

这样优化器就可以使用birth_date

LIMIT 33 -- 你关心哪 33 行? 也许你需要一个ORDER BY

当计划JOIN profile_details...可行时,不要执行JOIN ( SELECT... profile_details... )

SQL_CALC_FOUND_ROWS需要花费一些东西。 将其移除以查看其运行速度,然后决定是否值得保留。

我认为您不需要多次JOIN profile_details ,特别是因为它与profiles是 1:1 的。

这就是我的意思:

而不是JOIN ( SELECT... )刚刚

JOIN  profile_details AS d  USING(uid)

然后将这些添加到 WHERE 子句中:

AND  d.kids_pref = 1
AND  d.current_relationship = 1
AND  d.smoking_pref = 1 

避免文件排序

INDEX(country, gender,   -- Tested with '='
      birth_date,        -- Tested as a "range"
      uid)               -- For the ORDER BY -- Useless!

构建索引时,按此顺序包含列

  1. 所有列测试为“列 = 常数”。
  2. 一个范围(例如BETWEEN )。 如果这与ORDER BY相同,则可能会避免使用“文件排序”。

如果WHERE中没有“范围”,则

  1. 所有列测试为“列 = 常数”。
  2. ORDER BY列——假设它们都是DESC或所有ASC (或者,在 MySQL 8.0 中,匹配INDEX定义)。 这可能会避免“文件排序”。

但是索引不能同时处理“范围”和不同的“排序依据”。 考虑以下。 您有一个包含姓氏和名字的人员列表。 查询是

SELECT ...
    WHERE last_name LIKE 'Ja%'   -- a "range"
    ORDER BY first_name;

INDEX(last_name, first_name)将有助于WHERE ,但会使 first_names 混乱。 反之亦然。

(这是一种简化,有关更多详细信息,请参阅http://mysql.rjweb.org/doc.php/index_cookbook_mysql 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM