Query optimization with LIKE statement

John

Create a table like this:

CREATE TABLE test ( a INT(10), b char(10));

add two indexes:

alter table test add key aa (a), add key bb (b);

insert some rows:

insert into test values (132,'logi');
insert into test values (322,'koko');
insert into test values (32,'kola');

etc...

You can test with a little number of rows BUT in my application i will have a hundred million of rows so what i am going to ask makes sense: So, let's say that i want to execute two queries:

1) explain select * from test where b like "frif%";
2) explain select * from test where a like "32%";

The first uses the index bb since b is a char, while the second doesn't use the index aa since a is just an INT (and i treat it like a char!). How can i make the second query execute as the first and search for numbers like "some_number%" and make that a query of range type and not ALL (as explain shows).

Gordon Linoff

If you are treating the number as a character string, then store it that way. It sounds like the number is a code of some sort. Although consisting of numbers, it is really just the name of something (such as an account number).

If the number is fixed length, say 5, then you can do:

where a >= 32000 and a < 33000;

You could extend this idea for different lengths:

where a >= 32 and a < 33 or
      a >= 320 and a < 330 or
      a >= 3200 and a < 3300 or
      a >= 30000 and a < 33000 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framkework 'like' statement query

From Dev

SQL query LIKE statement error

From Dev

MySQL query case statement in where optimization

From Dev

CodeIgniter where and like sql query statement

From Dev

Query with LIKE statement for any of the words, not whole

From Dev

Spring Data and native query with like statement

From Dev

CodeIgniter where and like sql query statement

From Dev

Query regarding LIKE clause and Select Statement in SQLworkbench

From Dev

Query with LIKE statement for any of the words, not whole

From Dev

How to define Query within LIKE statement

From Dev

SQL query using Like and Between in the same statement

From Dev

azure Mobile app query like sql Statement

From Dev

MySQL search query optimization: Match...Against vs %LIKE%

From Dev

Query optimization?

From Dev

Query optimization?

From Dev

Clarification on javax persistence named query using the like statement

From Dev

How do I run an SQL update query using a like statement

From Dev

Android SQLite like query with multiple, optional conditions as a prepared statement

From Dev

How do I run an SQL update query using a like statement

From Dev

Using SOQL to query Identities using LIKE statement/keyword

From Dev

Android SQLite like query with multiple, optional conditions as a prepared statement

From Dev

Codeigniter: Query is returning all rows in table on select/like statement

From Dev

Contains Statement Like the Like statement

From Dev

Contains Statement Like the Like statement

From Dev

Image Optimization like TinyPNG

From Dev

switch statement optimization (Swift)

From Dev

JavaScript if statement optimization

From Dev

(Java) if statement optimization

From Dev

Optimization - For statement and variable declarations

Related Related

  1. 1

    Entity Framkework 'like' statement query

  2. 2

    SQL query LIKE statement error

  3. 3

    MySQL query case statement in where optimization

  4. 4

    CodeIgniter where and like sql query statement

  5. 5

    Query with LIKE statement for any of the words, not whole

  6. 6

    Spring Data and native query with like statement

  7. 7

    CodeIgniter where and like sql query statement

  8. 8

    Query regarding LIKE clause and Select Statement in SQLworkbench

  9. 9

    Query with LIKE statement for any of the words, not whole

  10. 10

    How to define Query within LIKE statement

  11. 11

    SQL query using Like and Between in the same statement

  12. 12

    azure Mobile app query like sql Statement

  13. 13

    MySQL search query optimization: Match...Against vs %LIKE%

  14. 14

    Query optimization?

  15. 15

    Query optimization?

  16. 16

    Clarification on javax persistence named query using the like statement

  17. 17

    How do I run an SQL update query using a like statement

  18. 18

    Android SQLite like query with multiple, optional conditions as a prepared statement

  19. 19

    How do I run an SQL update query using a like statement

  20. 20

    Using SOQL to query Identities using LIKE statement/keyword

  21. 21

    Android SQLite like query with multiple, optional conditions as a prepared statement

  22. 22

    Codeigniter: Query is returning all rows in table on select/like statement

  23. 23

    Contains Statement Like the Like statement

  24. 24

    Contains Statement Like the Like statement

  25. 25

    Image Optimization like TinyPNG

  26. 26

    switch statement optimization (Swift)

  27. 27

    JavaScript if statement optimization

  28. 28

    (Java) if statement optimization

  29. 29

    Optimization - For statement and variable declarations

HotTag

Archive