473,770 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding Order by slows my query!

I have the following query that runs quickly (0.5 secs):

select ZipListMatrix.Z ipListMatrixID, ZipListMatrix.Z ipID,
ZipListMatrix.C arrierRouteID,Z ipListMatrix.Li stID,
ZipListMatrix.Q uantity,List.Di splayOrder
from ZipListMatrix
join List
join UserList on List.ListID = UserList.ListID
on ZipListMatrix.L istID = List.ListID
where UserList.UserIn foID = 869
and ZipListMatrix.Z ipID = 23112
order by ZipListMatrix.Z ipID

But as soon as I change the order by to "order by ZipListMatrix.Z ipID,
ZipListMatrix.L istID" the query runs very slow (7.5 secs).

The table ZipListMatrix has 3.3 million records. I have indexes on ZipID,
ListID

When I do an "explain".. .I see the dreaded: Using Temporary, Using Filesort

Can anyone help? I don't know what to do to speed up the query!
-bruce duncan
Jul 20 '05 #1
4 2514
Bruce D wrote:
But as soon as I change the order by to "order by ZipListMatrix.Z ipID,
ZipListMatrix.L istID" the query runs very slow (7.5 secs).

The table ZipListMatrix has 3.3 million records. I have indexes on ZipID,
ListID

When I do an "explain".. .I see the dreaded: Using Temporary, Using Filesort

Try creating a single index containing both ZipID and ListID rather than
depending on the separate indices.
Jul 20 '05 #2
Bruce D wrote:
I have the following query that runs quickly (0.5 secs):
...order by ZipListMatrix.Z ipID

But as soon as I change the order by to "order by ZipListMatrix.Z ipID,
ZipListMatrix.L istID" the query runs very slow (7.5 secs).


What happens if you ORDER BY ZipListMatrix.L istID, without ordering by
ZipID? I'm wondering if you've created an index on UserList.ListID , and
forgotten to create one on the same field in ZipListMatrix.

http://dev.mysql.com/doc/mysql/en/OR...imization.html

This page describes cases where the ORDER BY must use a filesort.
The first case sounds like yours: ordering by two different keys.

That page also has a few suggestions to speed up filesorts.
You could also try creating a RAMdisk on your server, and point the
"tmpdir" option for mysqld to use that as its sorting space.

Another idea: create another column in ZipListMatrix that is a sensible
concatenation of ZipID and ListID, create an index on it, and then order
by that. By sensible, I mean concatenate in such a way that an alpha
sort on the result gives you the order you want.

Regards,
Bill K.
Jul 20 '05 #3
"2metre" <2m****@xxxhers ham.net> wrote in message
news:ck******** **@hercules.bti nternet.com...
Try creating a single index containing both ZipID and ListID rather than
depending on the separate indices.


I created an index, X1, on the ZipID and ListID fields. When I ran the
query...it was still very slow.

Query:
select ZipListMatrix.Z ipListMatrixID, ZipListMatrix.Z ipID,
ZipListMatrix.C arrierRouteID, ZipListMatrix.L istID, ZipListMatrix.Q uantity,
List.DisplayOrd er
from ZipListMatrix use index (X1)
join List
join UserList on List.ListID = UserList.ListID
on ZipListMatrix.L istID = List.ListID
where UserList.UserIn foID = 869
and ZipListMatrix.Z ipID = 23112
order by ZipListMatrix.Z ipID, ZipListMatrix.L istID

Explain:

UserList: ref: ListID,UserInfo ID: UserInfoID: 5: const: 3: Using
where; Using temporary; Using filesort:
List: eq_ref: PRIMARY,ListID: PRIMARY: 10: UserList.ListID : 1: :
ZipListMatrix: ALL: X1: NULL: NULL: NULL: 3376287: Using where:

Why didn't it use the X1 index?
-bruce
Jul 20 '05 #4
>
Why didn't it use the X1 index?
-bruce


Got it figured out!

It didn't have anything to do with the ORDER BY...
it was my fault when writing the "ZipListMatrix. ZipID = 23112"...ZIPID is a
character field...so MySQL was trying to compare the character field with
23112.
I changed the query from:
and ZipListMatrix.Z ipID = 23112
to
and ZipListMatrix.Z ipID = "23112"

and the query was back to subseconds!

-bruce
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3847
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for example "order by timeleft desc, gamenumber desc" actually speeded up the queries and reduced sql server 2000 timeouts. I have an ASP error log and I am fairly sure that a dramatic reduction in sql server timeouts is simply attributed to adding an...
4
3864
by: David Link | last post by:
Hi, Why does adding SUM and GROUP BY destroy performance? details follow. Thanks, David Link s1.sql: SELECT t.tid, t.title, COALESCE(s0c100r100.units, 0) as w0c100r100units,
2
2622
by: David | last post by:
Hi, I have an order form which has a field 'ProductID'. This form has a button on each record to open a new form linked by ProductID. This new form is a continuous form and obviously, only displays the records associated with that ProductID. Basically this is part of a Product Packing Pick System. From my products table, each product has a set default amount of items which
6
2299
by: Larry R Harrison Jr | last post by:
I have a database I'm designing in Access 97. I have a custom field in a query which looks in {Table of Documents} and shows them all. It then needs a "latest revision number," stored in another table named {Table of Revisions}. It naturally matches them up by linking the autoid in {Doc} with the related field in {Rev}. It then looks for a field in {Rev} called "revision number" and looks for the last one for the given Doc (linked by the...
2
5664
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow, DataTable.Rows.Add and DataAdapter.Update member functions. Before adding the new row object to the Rows collection, all of the row's fields that do not accept NULL must be assigned, otherwise an exception is thrown. However, I do not want to assign...
4
1471
by: Ron | last post by:
Hi all, I've got a frmCustomer form (designed via access 2000 form wizard) that uses a qryCustomerName query (also designed by query wizard) ordered by customer last name, first name, mi. Of course, it adds records to the end of the dataset rather than putting new entries into alpha order. And, there they sit until I leave the form and re-enter it. Is there a way to input the new record so that once it's entered it pops it into...
6
13573
by: Bob Stearns | last post by:
I am getting unwanted duplicate rows in my result set, so I added the DISTINCT keyword to my outermost SELECT. My working query then returned the following message: DB2 SQL error: SQLCODE: -214, SQLSTATE: 42822, SQLERRMC: CASE...;ORDER BY;2 Message: An expression in the ORDER BY clause in the following position, or starting with "CASE..." in the "ORDER BY" clause is not valid. Reason code = "2".  More exceptions ... DB2 SQL error:...
14
18752
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe is that as more and more are added, population of the list box takes longer and longer. ie the first 10th of the item set are added much much quicker than the last 10th. THis occurs with about 40,000 listbox items. My worry is the listbox may...
1
3767
by: 28490 | last post by:
Hi, In 10.2 when a query is run with GROUP BY it does not order the output by the grouped columns. In earlier versions the output was always sorted by the the grouped columns even though there was no ORDER BY clause present. In order to get an ordered output,decided to add ORCDER BY clause explicitly. But gave ORA-00904: invalid column name error for following query.
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10008
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7420
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3974
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 we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.