Jump to content

Slow SQL query


stubarny

Recommended Posts

Hello,

 

I run the following query 80 times in a loop:

 

$query ="SELECT DISTINCT website_page_name FROM website_page_names ORDER BY website_page_name ASC LIMIT " . $sql_query_line_item_number . ", 1";

 

The table has about 600,000 records. If the values used for $sql_query_line_item_number are low then it takes about 4 seconds to run all 80 queries.

 

If the values used for $sql_query_line_item_number are high then it can take about 60-90 seconds to run all 80 queries (i.e. about 1 second per query).

 

Is there a way to massively speed this up when large values are used for $sql_query_line_item_number? (I have a unique index set up on field 'website_page_name' so I thought this would have made it fast?)

 

Many thanks,

 

Stu

Link to comment
Share on other sites

From the look of it your completely miss-using the LIMIT statement.

 

If you want to get a specific website page name you should use a WHERE clause. If you want to select all the website page names and cycle through each one you can use the DISTINCT function and then cycle through your result set....

 

Running the same query 80 times is absolutely obscene!

Link to comment
Share on other sites

Hi Jesie,

 

Thanks but I need to find the first and last record in 40 ranges, so I'm not sure I can avoid a loop?

 

Stu

$sql = "SELECT name FROM companies ORDER BY name";
$names = array();
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    $names[] = $row['name'];
}
//Splits it into arrays of every 40 names.
$name_groups = array_chunk($names, 40);
foreach($name_groups AS $name_group){
    //Echo the first name, a dash, the last name, then a space pipe space as a separator.
    echo array_shift($name_group).'-'.array_pop($name_group);
    echo ' | ';
}

 

Not tested.

Link to comment
Share on other sites

6 seconds? Memory limit?Something else is wrong.

Well, jesirose's code retrieves all 600,000 rows of data from the table, then spits out all of the rows in chunks of 40. I'm a little surprised this only took 6 seconds, although I assume it's because of the memory limit he put in.

 

Anyways, with a table this big, you should be taking advantage of caching as much as possible, for both MySQL and PHP.

 

To retrieve 40 rows of data from anywhere in the table, use the following query:

SELECT DISTINCT website_page_name FROM website_page_names ORDER BY website_page_name ASC LIMIT 40 OFFSET {row start}

Replace {row start} with the row number to start at. So if the offset is 100 and limit is 40, it will retrieve only rows 100-140.

 

I must still warn you that while offset will help a lot in this scenario, it isn't perfect. When you use offset, it still iterates through all the previous rows, starting at 0; it just doesn't pay any attention to them when showing the result. Therefore, an offset of 500,000 will still be quite slow. I found an informative article about this issue which you may be interested in reading: http://www.4pmp.com/2010/02/scalable-mysql-avoid-offset-for-large-tables/

You may want to look at the design of your entire database if you really want to completely solve this issue.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.