473,473 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trying to optimize a MySQL query.

4 New Member
Okay, well what my query looks like is:

Expand|Select|Wrap|Line Numbers
  1. SELECT id, title FROM myTable LIMIT 0, 500 
This query is taking a long time and changes in time each time i run it, from 0-10 seconds.

And I don't see any reason behind it. id is set as my PRIMARY index.

The table has 132,000 rows, but I don't think it should be taking this long. Do you think the server is just slow? Or do you see a problem with my SQL query?

Thanks for any response. Also if you think it's a problem with my server, could you recommend a database host that would be faster?
Feb 18 '07 #1
5 1926
Atli
5,058 Recognized Expert Expert
Hi.

There is nothing wrong with that query.
It is possible that your server is slow, or that your connection is slow.

I'm using a server from bluehost.com that works pretty well and doesn't cost to much.
Feb 18 '07 #2
giraph
4 New Member
I don't think it's a connection problem, because when running it from just phpmyadmin I get...

Showing rows 0 - 499 (500 total, Query took 10.9797 sec)

If I use EXPLAIN I'm getting

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE myTable ALL NULL NULL NULL NULL 132306

(Sorry that didn't come out well) but it says it's looking through all 132000 rows which seems weird?

What I don't understand is if I run some of my other queries, that are just a simple SELECT * FROM myTable WHERE id=5 they take 1/100 of a second. I mean I can count the first 500 rows and return them faster than my database is doing it, does it matter about the other data in the row? I think it may be because my host uses a shared mysql environment, but I think it has to do with my query, since other queries are performed fine. Any more help would be great.


ALSO:

This query takes less than a second

SELECT * FROM `myTable` WHERE id <= 500

I think there is a problem with the LIMIT?
Feb 18 '07 #3
giraph
4 New Member
Okay I'm just gonna use the < and <= things because LIMIT seems to suck horribly. If anyone can tell me a reason behind this please let me know.

And thanks atli for responding =].
Feb 18 '07 #4
Atli
5,058 Recognized Expert Expert
I've check this out and it appearers that the LIMIT clause suffers from performance issues.

From what I've read, queries that have both the index and offset set will cause a full scan of the table.

Lets say we have a table with 100,000 entries.
This query will read the first 50 entries and then stop, finishing quickly.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM mytable LIMIT 50
While this one will go through the entire table, and throw away the first 99,950 records before returning the final 50, which in large tables, will run slowly.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM mytable LIMIT 99950 50

So I believe it is possible to rewrite your query and avoid the performance issue like so.
Expand|Select|Wrap|Line Numbers
  1. SELECT id, title FROM mytable LIMIT 500
Btw. If you use the WHILE statement like you posted, if you have or will ever delete a record with a id less than 500, that query wont return 500 records.
Feb 18 '07 #5
giraph
4 New Member
Well, the reason I used the LIMIT 0, 500 is because sometimes I start at 500, 500
1000, 500 etc. So the LIMIT was really causing a performance strain. It seems to be okay now, I'll just try not to delete any rows =] (hopefully).
Feb 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jy2003 | last post by:
I have read a book, which suggests we should change OR to UNION for better performance, but currently I have too many OR clauses(I have a query with 100 ORs) and it does not sound good to have 100...
0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
6
by: Bruce D | last post by:
Could someone please help to explain why the following query isn't using the index... explain select id from kbm where state = 'MA' table type possible_keys key key_len ref rows Extra...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
2
by: kshirsagar007 | last post by:
friends, I would like to optimize the following query....as its taking 2 minutes to get the records. select a.CODE "Code", ud.Name "Name", a.SchemeId "SchemeID" from (MASTER sh,...
11
by: bravo | last post by:
hi i need to optimize the join query, which joins three tables say table1 ,table2 , table3 each having huge volume of records... the query is as select table1.id,table2.time,table3.Status from...
1
by: acornejo | last post by:
Hi All I've the following code I need to optimize. Currently tblOutgoing is about 250K registers and growing at a rate of about 20k records per day. This code takes me over 5 secs to run on each...
3
zabsmarty
by: zabsmarty | last post by:
Can any one help me to make my query code optimize and load faster. Please help me or any example what steps should we use to optimize. Thank You
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.