【问题标题】:Mysql query using straight_join causing less cost but more time to execute使用 direct_join 的 Mysql 查询导致成本更低但执行时间更长
【发布时间】:2018-05-11 05:39:34
【问题描述】:

我正在尝试优化以下查询,该查询大约需要 1.5 秒才能执行。

SELECT  id
    FROM  leads
    left join  leads_cstm  ON leads.id = leads_cstm.id_c
    WHERE  deleted=0
      and  cust_temp_id_c = 'xxxx';

*************************** 1. row ***************************

           id: 1
  select_type: SIMPLE
        table: leads_cstm
   partitions: NULL
         type: ALL
possible_keys: PRIMARY
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 696334
     filtered: 10.00
        Extra: Using where
*************************** 2. row ***************************

           id: 1
  select_type: SIMPLE
        table: leads
   partitions: NULL
         type: eq_ref
possible_keys: PRIMARY,idx_del_user,idx_leads_id_del
          key: PRIMARY
      key_len: 108
          ref: crmsuite.leads_cstm.id_c
         rows: 1
     filtered: 50.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)

我尝试在leads_cstm(id_c,cust_temp_id_c) 上创建索引,但没有成功!!我也尝试使用 direct_join 并降低了成本,但查询现在需要更多时间来执行(3 秒)。

mysql>  EXPLAIN     SELECT  id
    FROM  leads
    STRAIGHT_JOIN  leads_cstm  ON leads.id = leads_cstm.id_c
    WHERE  deleted=0
      and  cust_temp_id_c = 'xxxxx';


*************************** 1. row ***************************

           id: 1
  select_type: SIMPLE
        table: leads
   partitions: NULL
         type: ref
possible_keys: PRIMARY,idx_del_user,idx_leads_id_del
          key: idx_del_user
      key_len: 2
          ref: const
         rows: 375820
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************

           id: 1
  select_type: SIMPLE
        table: leads_cstm
   partitions: NULL
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 108
          ref: crmsuite.leads.id
         rows: 1
     filtered: 10.00
        Extra: Using where
2 rows in set, 1 warning (0.00 sec)

请说明为什么使用直接连接需要更多时间以及如何优化此查询。

两个表都包含大约 750000 行。

【问题讨论】:

  • 请修正您的格式。
  • 完成蒂姆........
  • deletedcust_temp_id_c 在哪些表?没有这些信息就无法帮助优化。
  • LEFT JOIN 而不是 JOIN 的某些原因?

标签: mysql database indexing query-optimization database-administration


【解决方案1】:

我假设deleted 位于leads_cstm 中的leadscust_temp_id_c 表中。

要优化此查询,请尝试添加这两个索引:

ALTER TABLE `leads` ADD INDEX `leads_idx_deleted_id` (`deleted`,`id`);
ALTER TABLE `leads_cstm` ADD INDEX `leads_cstm_idx_c_c` (`cust_temp_id_c`,`id_c`);

然后运行查询:

SELECT
        leads.id 
    FROM
        leads 
    LEFT JOIN
        leads_cstm 
            ON leads.id = leads_cstm.id_c 
    WHERE
        leads.deleted = 0 
        AND leads_cstm.cust_temp_id_c = 'xxxx'

【讨论】:

  • 虽然没有创建上述索引,但仅在 cust_temp_id_c 列上创建索引提高了性能。感谢您的帮助!
  • 这很奇怪,因为上面建议的索引leads_cstm_idx_c_c 还包含cust_temp_id_c 作为第一列,所以它应该与只有cust_temp_id_c 的索引具有相同的效果。无论如何,很高兴你解决了这个问题,祝你好运:)
猜你喜欢
  • 2020-04-27
  • 2021-03-08
  • 2014-07-07
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2014-12-10
  • 1970-01-01
相关资源
最近更新 更多