简体   繁体   English

MySQL不使用索引

[英]Mysql doesn't use index

I tried to make indexes on my table. 我试图在桌子上做索引。 But First mysql uses it then that doesn't use the index. 但是首先mysql使用它,然后不使用索引。

It is my table indexes. 这是我的表索引。 enter image description here 在此处输入图片说明

My Query. 我的查询。

EXPLAIN SELECT
COUNT(DISTINCT K.bayiid) AS toplam,
SUM(K.tutar) AS yatirilan,
SUM(IF(K.durum='2', K.tutar*K.toplam,0)) AS kazanc,
SUM(IF(K.durum='-1', K.tutar, 0)) AS kayip,
SUM(IF(K.durum='1', K.tutar,0)) AS devam,
SUM(IF(K.durum='0', K.tutar,0)) AS iptal,
SUM(1) AS oynanan,
SUM(IF(K.durum='2', 1,0)) AS kazanan,
SUM(IF(K.durum='-1', 1,0)) AS kaybeden,
SUM(IF(K.durum='1', 1,0)) AS devameden,
SUM(IF(K.durum='0', 1,0)) AS iptalolan,
U.*
FROM kuponlar AS K 
INNER JOIN users AS U ON U.id = K.bayiid AND U.durum != '4' AND U.id = '26689'
WHERE K.durum < 3   AND K.tarih >= '2016-12-01 00:00:00' AND K.tarih <= '2016-12-31 23:59:59'

Query explaination enter image description here 查询说明在此处输入图像描述

But i don't want to use USE INDEX or FORCE INDEX. 但是我不想使用USE INDEX或FORCE INDEX。 Do you have any idea why mysql works unstable? 您知道为什么mysql工作不稳定吗?

 ON U.id = K.bayiid 
AND U.durum != '4'
AND U.id = '26689'

For clarity, move the last 2 parts to WHERE ; 为了清楚起见,将最后两部分移到WHERE leave only the condition(s) that defines the JOIN . 仅保留定义JOIN

WHERE K.durum < 3 
  AND K.tarih >= '2016-12-01 00:00:00'
  AND K.tarih <= '2016-12-31 23:59:59'

Suggestion: For date ranges, do something like this: 建议:对于日期范围,请执行以下操作:

  AND K.tarih >= '2016-12-01'
  AND K.tarih  < '2016-12-01' + INTERVAL 1 MONTH

For K , have 对于K

INDEX(bayiid, darum)
INDEX(bayiid, tarih)

(Without knowing the distribution of the data, I cannot predict which is better. The Optimizer will choose.) (在不知道数据分布的情况下,我无法预测哪个更好。Optimizer将选择。)

If you want to discuss this further, please provide SHOW CREATE TABLE for each table. 如果您想进一步讨论,请为每个表提供SHOW CREATE TABLE If there happen to be datatype inconsistencies, that could cause a lot of trouble, and we cannot see it without the SHOW . 如果数据类型不一致,可能会造成很多麻烦,没有SHOW我们将看不到它。

Indexing cookbook 索引食谱

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

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