Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing index with range type query and other criteria
    primarykey
    data
    text
    <p>I need help with this. I have such query:</p> <pre><code>SELECT * FROM cart WHERE (ts_in between 1249077600 AND 1318975199); </code></pre> <p>The above query is using btree index on ts_in colomn (range type). Now I want to add another criteria to this. For example:</p> <pre><code>SELECT * FROM cart WHERE (ts_in between 1249077600 AND 1318975199) and is_removed=0; </code></pre> <p>The above query doesn't use the btree index on ts_in colomn (range).</p> <p>Can somebody tell me why and how can I do it right way to reach very fast calculations.</p> <p>I created two indexes for testing :</p> <pre><code>CREATE INDEX range_idx_1 using BTREE ON cart (is_removed, ts_in); </code></pre> <p>and</p> <pre><code>CREATE INDEX range_idx_2 using BTREE ON cart (ts_in , is_removed); </code></pre> <p>What is Funny that when I use this query:</p> <pre><code>EXPLAIN SELECT id FROM cart WHERE (ts_in between 1249077600 AND 1318975199) AND is_removed=0; </code></pre> <p>I receive this results:</p> <pre><code>id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 1 | SIMPLE | cart | range | range_idx_1,range_idx_2 | range_idx_1 | 6 | | 17391 | Using where; Using index </code></pre> <p>The above query is using index but:</p> <pre><code>EXPLAIN SELECT * FROM cart WHERE (ts_in between 1249077600 AND 1318975199) AND is_removed=0; </code></pre> <p>I have such results:</p> <pre><code>id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra 1 | SIMPLE | cart | ref | range_idx_1,range_idx_2 | range_idx_1 | 1 | const | 77979 | Using where </code></pre> <p>This doesn't use the index.</p> <p>When I try to USE INDEX or FORCE INDEX syntax the results are the same. In one case Mysql doesn't use index. Any Help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload