473,729 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimising SQL Queries?

Is there any (simple? har!) way to optimise a particular SQL query?

At this stage, I'm more interested in making the query more readable, so
I've started hunting down references about relational theory to see if I
can use relational algebra to manipulate the query into a "shorter" form
(I believe the mathematical term is "simplify") .

Since I've got somewhere between two days and two lifetimes' worth of
reading ahead of me, would it be too presumptious to ask the list if
anyone can recommend a method, tool or book that would help me out?

At present, the one query I'm struggling with is a mish-mash of unions,
uniques and subselects. I'm sure there's an easier way, but I just can't
get the tables in my head to morph themselves the way I want them to.
The "explain" of my query is 38 rows long!

In my perfect world, there'd be some tool available where I could
provide the table definitions and my sample query, at which time the
tool would spit out a magically optimised query that provides exactly
the same results with far fewer linear scans, subselects and unique
filters. Heck... perhaps it could even suggest what indices I should add
to my tables to improve the query plan.

But it's not a perfect world, and I'm just a clueless newbie in the land
of relational theory, with no idea of the caveats that cover trying to
apply relational theory to real-world SQL!

Any suggestions on general strategies in producing sql queries? eg: is
it better to apply filters closer to the data or closer to the end
result table ("deeper" or "shallower" in the subselect tree)?

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 22 '05 #1
1 2950
Hmm, relational algebra is not always straight, and anyway, many people
complain that SQL doesn't really conform to relational algebra anyway.

The usual things to check are (off the top of my head):

* Are you using UNION where UNION ALL would be more appropriate (saves a
unique)
* Are the subselects that can be pulled into joins (postgresql is pretty
good with these).
* Are you using IN on an older version of PostgreSQL.
* Are there joins with differing types that could cause non-optimal use of
indexes.
* Partial indexes

Remember, just because PostgreSQL isn't using the index now doesn't mean it
won't when a table gets to a significant size.

Hope this helps,

On Wed, Jan 14, 2004 at 03:03:45PM +1100, Alex Satrapa wrote:
Is there any (simple? har!) way to optimise a particular SQL query?

At this stage, I'm more interested in making the query more readable, so
I've started hunting down references about relational theory to see if I
can use relational algebra to manipulate the query into a "shorter" form
(I believe the mathematical term is "simplify") .

Since I've got somewhere between two days and two lifetimes' worth of
reading ahead of me, would it be too presumptious to ask the list if
anyone can recommend a method, tool or book that would help me out?

At present, the one query I'm struggling with is a mish-mash of unions,
uniques and subselects. I'm sure there's an easier way, but I just can't
get the tables in my head to morph themselves the way I want them to.
The "explain" of my query is 38 rows long!

In my perfect world, there'd be some tool available where I could
provide the table definitions and my sample query, at which time the
tool would spit out a magically optimised query that provides exactly
the same results with far fewer linear scans, subselects and unique
filters. Heck... perhaps it could even suggest what indices I should add
to my tables to improve the query plan.

But it's not a perfect world, and I'm just a clueless newbie in the land
of relational theory, with no idea of the caveats that cover trying to
apply relational theory to real-world SQL!

Any suggestions on general strategies in producing sql queries? eg: is
it better to apply filters closer to the data or closer to the end
result table ("deeper" or "shallower" in the subselect tree)?



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)
--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ (... have gone from d-i being barely usable even by its developers
anywhere, to being about 20% done. Sweet. And the last 80% usually takes
20% of the time, too, right?) -- Anthony Towns, debian-devel-announce


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFABNjJY5T wig3Ge+YRAgobAJ 9zu48QvN3Pb1rmw upQNeQxOntbZgCg i8hc
MfqGnzG3uiaa+9u E32agK9k=
=+ukQ
-----END PGP SIGNATURE-----

Nov 22 '05 #2

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

Similar topics

1
3836
by: Howie Goodell | last post by:
Hello -- I am trying to optimise a JDBC connection and an Oracle 9i database for reading millions of records at a time to a 1 Gig PC running Win2K or XP. Two questions: 1. Does anyone have suggestions for optimising an Oracle 9i server (Enterprise Edition, Release 2) for networked read-only JDBC access with large return sets? With default settings MySQL reads 1M records 2.5 times faster than Oracle even on its first, non-cached...
3
1504
by: Andrew | last post by:
Hi, I have a problem I would really appreciate help with. I am generating dynamic SQL and need to optimise it. The specific example I am trying to optimise looks like this: SELECT DISTINCT DataHeaderID FROM TB_DataDetailText T1 WHERE (EntityFieldID IN ( 31) AND (Data LIKE '12BORE%' )) AND (DataHeaderID=(SELECT DISTINCT DataHeaderID FROM TB_DataDetailText CT2 WHERE T1.DataHeaderID = CT2.DataHeaderID AND (EntityFieldID IN ( 34) AND...
5
4040
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I think - it might have been XP) to 2003. The database is impressive, both in what it does and the obtuse and inconsistent ways it works. There are several hundred queries, for example, with no indication of where they are used or if they are in fact...
4
3535
by: Paul Brown | last post by:
Thanks for the replies Tristan, Eric, Steven & Kurt. They have given me some good leads. I present justification for a lot of the comments that drew (constructive) criticism below. Firstly, let me summarise the feedback that has pointed me in the right direction :- >> Steven > 20.13: What's the best way of making my program efficient? > A: By picking good algorithms and implementing them carefully.
4
1280
by: Jon Maz | last post by:
Hi, I just read the following in an old NG thread: "when you use the adapter with a closed connection it will open it, do your requested database access, and close it immediately. If you use the adapter with an open connection the connection will continue to be open after the adapter is done." My DataLayer makes extensive use of DataAdapters to fill DataSets, and
5
1454
by: Chris Weston | last post by:
Hi. Maybe I'm just being dim, but I'm struggling to get my head around optimising a query with regard to indexes. If I make a select query, such as a pseudo-example 'select * from bigtable where foo='bar' and (barney>rubble and fred<flintoff)', and the table is indexed on 'foo', how could I make that any better? What indexes could I add, or what could I change in the query? I know it looks simple, but so am I.
2
1800
by: Eckhart | last post by:
Dear All, Plz help me in optimising the following query, Reduce repeatable reads from the table via select ,ythe table sare not having referntial integrity constarints ,relations CREATE proc Rolex136Sync as DECLARE @date varchar(50),@ydate varchar(50) print CONVERT(char(11),(GETDATE()-1),100) SET @date =...
7
1370
by: Martin Eyles | last post by:
I have an ASP page that makes a vml/svg graph out of data. In order to extract all the necessary data I have to perform 68 SQL queries, which is unfortunatley a bit slow at the moment. For each of the procedures I go through code of this form Dim conn As New SqlClient.SqlConnection("Data Source=" & ServerName & "; User ID=***; Password=***") conn.Open() SQL = "" 'in operation has code to dynamically construct one of the
4
2509
by: PaulR | last post by:
Hi, (re: DB2 LUW v8.1 fp8 optimisation Level2) Can anyone explain why the following difference have wildly different plans - this seems very fundamental) select p.part_number, sp.supplier_part_number from jabs.supplier_part sp inner join jabs.part p on p.oid = sp.part_oid
0
9426
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...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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,...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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

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.