在Varchar列上设置MySql中的索引

时间:2014-07-27 16:26:14

标签: mysql database indexing sqlperformance sql-tuning

在MySql中,我有一个UNION查询需要很长时间才能执行。 查询看起来像

(SELECT id, X, Y , date FROM TABLE WHERE date in (SELECT MIN(date) FROM TABLE WHERE flag = 0 AND id = ? AND date BETWEEN ? AND ?) and id = ? and flag=0 ORDER BY id DESC LIMIT 1)

UNION 

(SELECT id, X, Y , date FROM TABLE WHERE date in (SELECT MAX(date) FROM TABLE WHERE flag = 0 AND id = ? AND date BETWEEN ? AND ?) and id = ? and flag = 0 ORDER BY id DESC LIMIT 1)

问题是为了获得上述查询的最佳性能,需要索引哪些列以及为什么?

id - type int
date - type datetime
flag - type tinyint(4)

1 个答案:

答案 0 :(得分:0)

我将索引编入索引 身份证明 2.约会 标志