473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inefficient query time with ORDERY BY clause, are there any alternatives?

Hey there,

I've been chasing a problem for the project that I'm working on and was able to narrow it down to the ORDER BY clause in my query.

The algorithm looks like this, I take a range of time selected by the user and I need to do some calculations on the items within that range of time. However, before I calculate, I need to query for the most recent row of a specified item before that time range. So my Query statement ends up looking something like this:

SELECT * FROM table WHERE ....... ORDER BY date_time DESC LIMIT 1.

I'm pretty sure the problem is the ORDER BY clause orders the whole db before doing my WHERE clauses and the LIMIT thus it takes an extremely long time when my db is decently large. This is not acceptable and renders the LIMIT clause almost useless with respect to saving time.

If anyone knows of a way to get around this that can boost my execution time, please let me know! Thanks.
Oct 31 '06 #1
2 1925
bharad
7
Creating an index on date_time attribute would help.

CREATE INDEX IDX_table_date_time ON table (date_time);

-bharad



Hey there,

I've been chasing a problem for the project that I'm working on and was able to narrow it down to the ORDER BY clause in my query.

The algorithm looks like this, I take a range of time selected by the user and I need to do some calculations on the items within that range of time. However, before I calculate, I need to query for the most recent row of a specified item before that time range. So my Query statement ends up looking something like this:

SELECT * FROM table WHERE ....... ORDER BY date_time DESC LIMIT 1.

I'm pretty sure the problem is the ORDER BY clause orders the whole db before doing my WHERE clauses and the LIMIT thus it takes an extremely long time when my db is decently large. This is not acceptable and renders the LIMIT clause almost useless with respect to saving time.

If anyone knows of a way to get around this that can boost my execution time, please let me know! Thanks.
Nov 1 '06 #2
Hey bharad, thanks for the post. I've looked into this matter for a while and all possible solutions point to creating an Index. So I have a few standard questions about db index (keep in mind I am using SQLite).

If I create an Index on date_time, would it modify the date_time column to exclude non-unique elements? Because I need to store multiple items with the same date stamp, does that effect the index?

On 2nd note, is there any way to check whether or not an index has already been created for the specified column? There's no point in creating an index for a column that already has one.

Thanks.


Creating an index on date_time attribute would help.

CREATE INDEX IDX_table_date_time ON table (date_time);

-bharad
Nov 14 '06 #3

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

Similar topics

7
by: vnl | last post by:
I'm trying to run a SQL query but can't find any records when trying to select a certain date. Here's the sql: SELECT field 1, field2, date_and_time, FROM table1 WHERE date_and_time =...
3
by: David | last post by:
Consider this SQL Query: ----------------------------------------------------------------- SELECT c.CASE_NBR, DATEDIFF(d, c.CREATE_DT, GETDATE()) AS Age, c.AFFD_RCVD, c.PRV_CRD_ISS, x.RegE,...
3
by: Andrew Mayo | last post by:
There is something very strange going on here. Tested with ADO 2.7 and MSDE/2000. At first, things look quite sensible. You have a simple SQL query, let's say select * from mytab where col1 =...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
3
by: dskillingstad | last post by:
I'd appreciate any help I can get. I'm not sure what I'm doing wrong, but.... I've searched these groups for some solutions but no luck. I have an unbound form (frmSearch), with several unbound...
2
by: Zygo Blaxell | last post by:
I have a table with a few million rows of temperature data keyed by timestamp. I want to group these rows by timestamp intervals (e.g. every 32 seconds), compute aggregate functions on the...
9
by: Philip Pinkerton | last post by:
When trying to connect to database via the pgAdmin3 GUI it asks for a password. I use the same passworrd as I did when I connect to the DB via command line but I get Ident error? how do I set,...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.