1

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.

6
  • 1
    You need to add a index to the link column Jul 7, 2013 at 14:39
  • 1
    Definitely needs index. Normalizing the link column might also be worth looking into.
    – Jon
    Jul 7, 2013 at 14:40
  • You can add a fultext index to your table: petefreitag.com/item/477.cfm
    – Gimmy
    Jul 7, 2013 at 14:42
  • You can change your data type from text to other. I recommend to you that you have to change it to varchar with smaller size. But how can so long? I never execute sql for more than 6.37sec before. Jul 7, 2013 at 14:43
  • Prefix that query with Explain, and post the output. We could guess at the answer, add an index on the link column, but 6 seconds to test 38 rows seems a trifle execessive. Jul 7, 2013 at 14:43

4 Answers 4

4

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.

2
  • What is your new query time?
    – Curt
    Jul 7, 2013 at 15:08
  • select query run within 1sec.
    – ash84
    Jul 7, 2013 at 15:08
2

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:

http://www.snip2code.com/Snippet/1171/List-of-Variables-to-be-set-in-MySQL-for?fromPage=1

1

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.