mysql索引for max with where

时间:2011-08-09 11:43:52

标签: mysql indexing

尝试获取最新的computed_date,其中normalized_global_score不为NULL

SELECT max(`influencer_brand_scores`.computed_date) AS max_computed_date
FROM `influencer_brand_scores` 
WHERE (normalized_global_score IS NOT NULL) 

此查询将永远存在,当我对其进行解释时,我得到:

  

1 SIMPLE influencer_brand_scores ALL(null)(null)(null)(null)3347895使用where

所以我的问题是,我是否在computed_date上添加了一个索引,或者在normalized_global_score和computed_date上添加了一个复合索引,如果是的话应该是第一个,还是重要?

2 个答案:

答案 0 :(得分:0)

ALTER TABLE `influencer_brand_scores` 
    ADD INDEX `ibs_cindex` (`normalized_global_score`, `computed_date`);

答案 1 :(得分:0)

我先跟

一起去
INDEX( normalized_global_score, computed_date )

如果这样不能正常工作(因为normalized_global_score包含NULL),请创建一个名为normalize_gobalcolumn_score_is_computed的新BOOLEAN列并索引:

INDEX( normalize_gobalcolumn_score_is_computed, computed_date )

并在查询中将normalized_global_score IS NOT NULL替换为normalize_gobalcolumn_score_is_computed = TRUE