473,757 Members | 10,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help optimising SQL Query

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_DataDetailTe xt T1 WHERE
(EntityFieldID IN ( 31) AND (Data LIKE '12BORE%' )) AND
(DataHeaderID=( SELECT DISTINCT DataHeaderID FROM TB_DataDetailTe xt CT2
WHERE T1.DataHeaderID = CT2.DataHeaderI D AND (EntityFieldID IN ( 34)
AND (Data LIKE 'SIDE BY SIDE%' )) ))AND
(DataHeaderID=( SELECT DISTINCT DataHeaderID FROM TB_DataDetailTe xt CCT3
WHERE T1.DataHeaderID = CCT3.DataHeader ID AND (( Data LIKE 'church%' ))))

I was OK optimising it with just 2 criteria and changed:
SELECT DISTINCT DataHeaderID FROM TB_DataDetailTe xt T1 WHERE

(EntityFieldID IN ( 31) AND (Data LIKE '12BORE%' )) AND

(DataHeaderID=( SELECT DISTINCT DataHeaderID FROM TB_DataDetailTe xt CT2

WHERE T1.DataHeaderID = CT2.DataHeaderI D AND (( Data LIKE 'church%' ))))

which took 26 seconds to using a derived table

SELECT distinct T1.DataHeaderID FROM TB_DataDetailTe xt as T1

inner join (SELECT distinct DataHeaderID, Data FROM TB_DataDetailTe xt )
CT2

on T1.DataHeaderID = CT2.DataHeaderI D

WHERE

(T1.EntityField ID IN ( 31) AND (T1.Data LIKE '12BORE%' ))

and (( CT2.Data LIKE 'church%' )) which took 0.03 seconds on the same data.

My problem is I need to write code to generate the SQL for 1 to n criteria
and am struggling to write the query for more than 2

Best regards,

Andrew
Jul 23 '05 #1
3 1505
Completely untested...(I have two lovely young ladies ripping up some
cardboard behind me which is about an annoying a noise as you can get,
so if it's wrong, you have my excuse. I started on this before they
starting on the cardboard).

I've tried to tidy up your code a bit in the hopes that this points you
in a better direction for solving the problem with n criteria. Maybe
someone could take this further.

SELECT
T1.DataHeaderId

FROM
TB_DataDetailTe xt T1
LEFT JOIN TB_DataDetailTe xt CT2
ON T1.DataHeaderId = CT2.DataHeaderI d
LEFT JOIN TB_DataDetailTe xt CCT3
ON CT2.DataHeaderI d = CCT3.DataHeader Id

WHERE
T1.EntityFieldI d = 31 AND
T1.Data LIKE '12Bore% AND
CT2.EntityField Id = 34 AND
CT2.Data LIKE 'Side By Side%' AND
CCT3.Data LIKE 'Church%'

Ryan

Jul 23 '05 #2
Hi Ryan,

Thanks very much. Tidying it up like that makes it much more straightforward
to see what I should do. I have it cracked now and yes, that does work and
blindingly fast (there was a bit of a whinge about quotes but that may have
been me and was easily fixed). It also lets me see a second way that I can
do it.

Thanks again,

Andrew
Jul 23 '05 #3
No problem.

Ryan
Andrew wrote:
Hi Ryan,

Thanks very much. Tidying it up like that makes it much more straightforward to see what I should do. I have it cracked now and yes, that does work and blindingly fast (there was a bit of a whinge about quotes but that may have been me and was easily fixed). It also lets me see a second way that I can do it.

Thanks again,

Andrew


Jul 23 '05 #4

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...
1
1799
by: Jasper Bryant-Greene | last post by:
I have a database of movie titles, with about 78,000 records, and a database of related people (directors, writers, actors/actresses etc.) with about 141,000 records. I display a random movie out of this database on each hit to my website's homepage. This worked fine when I had only a couple thousand movies, but now that the DB has grown, it seems to be taking a bit longer to process the page. My DB schema for each table:
4
3536
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.
1
1029
by: BobLaughland | last post by:
Hi All, I have a web page that loads data out of a database, and then displays it on the screen. There is a lot of logic behind the displaying, some times fields are made invisible, and sometimes they are hidden. The page is slow to load, but I must admit that the database query to load the page is only small, and will only bring back one row, so I am
5
1455
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.
7
1374
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
21
2328
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to solve in this small code, I can solve it on my own program...you don't want to have head-ache :-) So, the problem excatly is, I built an array..just like this one.
4
2512
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
1
1242
by: ananth | last post by:
Hi All, I have a java code that retrievies values from a table in a oracle database. There are more than 100 columns and 100000 records for the table in the database and i am able to retrieve 88 records for 5 mins which doesnt suit the requirement. I need to retrieve 300 records per minute is there any alternative available so that i can optimize the query or use any java methods to retrieve the values in the specified time ...
0
9489
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
9298
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
10072
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
9885
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
8737
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
7286
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
6562
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
5172
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...
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.