简体   繁体   中英

how to improve select performance in mysql?

I have 1034961 rows data from database(mysql) table.

structure..

table : tb_blog_image

    |   id(pk)   |   link(TEXT)   |   img_link(TEXT)   |
    |     1      |blogpost.com/aaa|http://sky.png      |
    |     2      |blogpost.com/aaa|http://girl.png     |
    |     3      |blogpost.com/aaa|http://cad.png      |

now, I want to select specific img_link list from tb_blog_images.

sql..

    select link, img_link
    from tb_blog_image
    where link = 'blogpost.com/aaa';

result 38rows 6.37sec

how to improve select performance? make link column to index? table normalization?

I want to run within 1sec.

I want to listen various tips.

Ideally, you'd use the primary key, (id) within your program.

But failing that, an index on the link column would solve your problem. Right now, the query you are running requires that all one million rows from the table be loaded from disk, and then compared to your query string value. Using an index, this could be reduced to no more than three or four disk reads.

Also, a text datatype is stored separately from the rest of the table, which is not very efficient. I would re-define the two text columns to something like varchar(300), which is certainly long enough for any URL that might be encountered, but provides for more efficient storage as well: the TEXT type is really for (potentially) long fields like memos, or the content of web pages.

You may be interested in the variables to be tuned for fine performances.

Set global thread_cache_size = 4;
Set global query_cache_size = 1024*1024*1024;
Set global query_cache_limit=768*1024;
Set global query_cache_min_res_unit = 2048;
Set long_query_time = 5;

Source:

you are selecting data from single table so improvement in execution time is only by index

It is possible to create multiple indexes on a table with one ALTER TABLE statement.This is relatively efficient, because the clustered index of the table needs to be scanned only once

For more detail see http://dev.mysql.com/doc/innodb/1.1/en/innodb-create-index-examples.html

I don't know, which language you are using other than MySQL, but instead of selecting both link and img_link column, you can select id .

The code will be something like this:

 select id
    from tb_blog_image
    where link = 'blogpost.com/aaa';

// Now Fetch The Rows

Echo the rows using a loop one by one {

// Inside this loop, do this 

  select link, img_link
    from tb_blog_image
    where link = $id 

// $id is a variable of the current row

 echo "whatever you want";

}

Let me know, which language you are using, I can help, if it is PHP.

Let me know, if it improved the performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM