English 中文(简体)
能否按距离选择最左边的多指数?
原标题:Is it possible to select by range on leftmost multiple-index?

I have an index m_idx on (k1,k2,k3)
If I do
SELECT c1,c2,c3... FROM tb FORCE INDEX (m_idx) WHERE k1=500 AND k2 IN(...) AND k2>2000 ORDER BY k1 LIMIT 1000;
OR
SELECT c1,c2,c3... FROM tb FORCE INDEX (m_idx) WHERE k1 IN (500,1000,1500 ...) AND k2 IN(...) AND k2>2000 ORDER BY k1 LIMIT 1000;
Handler_read_next = 999

BUT if I try to use a range on k1:
SELECT c1,c2,c3... FROM tb FORCE INDEX (m_idx) WHERE k1>=500 AND k2 IN(...) AND k2>2000 ORDER BY k1 LIMIT 1000;
Handler_read_next = 58035
In all cases EXPLAIN says that the key used is m_idx
But I think that in the third case m_idx it s not used (I also have an index only on k1).
Otherwise I don t understand why it is reading more than 1000 rows.
I was expecting to scan m_idx index, and ONLY the first 1000 rows that meet the conditions to be read from table.
But in fact I think that for the third case it scans the index and those rows who meet k1 condition are read from the tb and k2 and k3 conditions are checked after the rows are read from tb.
I use: MySql with MyISAM, WINDOWS 7 64, the tb has 1 mil rows;
So my questions are:
Is it possible to select by range on leftmost multiple-index?
OR
I m doing something else wrong?
Thank you.

最佳回答
  1. No it is not.
  2. No, you re doing everything right

rel=“nofollow”http://dev.mysql.com/doc/refman/5.1/en/range-optimization.html (7.3.1.3.2.) 多部分指数的“幅度办法”部分

因此,你不能帮助优化工作,以更快地完成这项任务。

避免使用<条码>FORCE INDEX,因为优化者更好地了解使用何种指数。

此外:

k1>=500 AND k2 IN(...) AND k2>2000

视哪一部分记录减少:k1>=500k2 IN(...)和k2><2000/code> (如果不确定为何需要<代码>>2000,则由于您可以人工比较,在添加<代码>IN(<>)之前,你还可以尝试制作索引<代码>k2<>>>(如果<编码>k2<>>>>>>>部分/代码>减去记录。)

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签