473,387 Members | 1,698 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,387 software developers and data experts.

Fastest way to query limited time ordered data set

Hi,

Suppose I have 2 tables. TableA has about 3 million rows and TableB has
about 50,000 rows. Assuming a btree index is used in the time column.

I need to query a limited and time ordered data in TableA that are not in
TableB. Because TableA can be huge, I only want to compare the top 1000 rows
ordered by time in TableA. What would be the fastest way to write the SQL
query?

Method 1
--------
SELECT * FROM TableA a
WHERE date_part('epoch', CURRENT_TIMESTAMP - a.col2) > 3600 AND
NOT EXISTS (SELECT b.col1 FROM TableB b WHERE b.col1 = a.col1 AND b.col2 >=
100)
ORDER BY a.col1 LIMIT 1000;

Method 2
--------
SELECT * FROM (SELECT * FROM TableA WHERE date_part('epoch',
CURRENT_TIMESTAMP - col2) > 3600 ORDER BY col1 LIMIT 1000) a
WHERE NOT EXISTS (SELECT b.col1 FROM TableB b WHERE b.col1 = a.col1 AND
b.col2 >= 100);

Method 3
--------
SELECT * FROM (SELECT * FROM TableA ORDER BY col1 LIMIT 1000) a
WHERE date_part('epoch', CURRENT_TIMESTAMP - a.col2) > 3600 AND NOT EXISTS
(SELECT b.col1 FROM TableB b WHERE b.col1 = a.col1 AND b.col2 >= 100);

Method 4
--------
SELECT * FROM (SELECT * FROM TableA ORDER BY col1 LIMIT 1000) a
WHERE date_part('epoch', CURRENT_TIMESTAMP - a.col2) > 3600 AND a.col1 NOT
IN (SELECT b.col1 FROM TableB b WHERE b.col2 >= 100);
There are a few more ways to write this (using views, temp tables), but what
would be fastest in PostgreSQL? Please suggest other SQL statements. Method
1 may not work because it may end up looping the entire TableA.

Thanks.


Nov 23 '05 #1
0 1389

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

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
14
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per...
9
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
4
by: John A Fotheringham | last post by:
I've probably not given the best title to this topic, but that reflects my relative "newbie" status. I have a table that goes essentially TS DATETIME jobnumber VARCHAR jobentry VARCHAR ...
4
by: laurenq uantrell | last post by:
I am trying to determine which of three stored procedure designs are fastest in the Query Analyzer: One query is a straight SELECT query with all desired rows and a dozen (tblName.RowName =...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
1
by: vunderusaf | last post by:
I have a listbox on a form that is selecting using named FinalQuery: SELECT ., ., . FROM FinalQuery; Now I have a text field with a date on this form and I'd like to use that date as the...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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,...
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...

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.