473,395 Members | 1,694 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.

SQL Query becomes too much

106 100+
HI All

My client requirements are such that I m going to generate query runtime in asp.net.

At some time the query reaches to very much length as below

Expand|Select|Wrap|Line Numbers
  1. SELECT tblJobPost.JobTitle,tblJobPost.JobDesc,Scraped,tblSchools.logoimage,tblJobPost.JobPostID,tblJobPost.SchoolID,tblSchools.web,cast(tblJobPost.MemType as nvarchar(255)) as MemType,tblSchools.InstitutionName,tblSchools.InstitutionName as Empname,tblJobPost.PayScale,cast(tblLEA.LEA as nvarchar(255)) as LEA,tblContract.ContractType,tblHoursAbl.HoursAbl,cast(tblJobPost.StartDate as nvarchar(255)) as StartDate, cast(tblJobPost.ClosingDate as nvarchar(255)) as ClosingDate,cast(tblJobPost.asap as nvarchar(50)) as asap, cast(tblJobPost.postdate as nvarchar(50)) as postdate FROM tblJobPost LEFT Outer JOIN tblpositions ON tblJobPost.position_id = tblpositions.position_id LEFT OUTER JOIN tblMainsubjects ON tblJobPost.SubjectID = tblMainsubjects.MSubjectID LEFT OUTER JOIN tblSchoolPhase ON tblJobPost.SchoolPhaseID = tblSchoolPhase.SchoolPhaseID LEFT OUTER JOIN tblLEA ON tblJobPost.LeaID = tblLEA.LeaID LEFT OUTER JOIN tblHoursAbl ON tblJobPost.HoursID = tblHoursAbl.HoursAblID LEFT OUTER JOIN tblContract ON tblJobPost.ContractID = tblContract.ContractID LEFT OUTER JOIN  tblregion ON tblJobPost.Region_id = tblregion.RegionID RIGHT OUTER JOIN  tblSchools ON tblJobPost.SchoolID = tblSchools.SchoolID Where (tblregion.region = 'London') and (tblSchoolPhase.SchoolPhase = 'Secondary Education') and (tblpositions.Position_title = 'Classroom Teacher')and (tblMainSubjects.MSubject = 'Science')and (tblLEA.LEA = 'Croydon') and (tblJobPost.ClosingDate >= '3/30/2009 11:14:49 PM')
  2. Union
  3. Select tblJobScrap.JobTitle,tblJobScrap.JobDesc,tblJobScrap.Scraped,tblSchools.logoimage,tblJobScrap.JobPostID,tblJobScrap.SchoolID,tblSchools.web,tblSchools.MemType,tblSchools.InstitutionName,tblJobScrap.InstitutionName as Empname,tblJobScrap.PayScale,tblJobScrap.LEA,tblJobScrap.Contract as ContractType,tblJobScrap.HoursAbl,tblJobScrap.StartDate,tblJobScrap.ClosingDate,tblJobScrap.asap,tblJobScrap.postdate from tblJobScrap LEFT OUTER JOIN tblSchools ON tblJobScrap.SchoolID = tblSchools.SchoolID  Where (((Region like '%London%' OR Region like 'London %' OR Region like '% London'))) AND NOT (((JobTitle like '%Primary%' OR JobTitle like 'Primary %' OR JobTitle like '% Primary') OR (JobTitle like '%Infant%' OR JobTitle like 'Infant %' OR JobTitle like '% Infant') OR (JobTitle like '%Junior%' OR JobTitle like 'Junior %' OR JobTitle like '% Junior') OR (JobTitle like '%Early%' OR JobTitle like 'Early %' OR JobTitle like '% Early') OR (JobTitle like '%Elementary%' OR JobTitle like 'Elementary %' OR JobTitle like '% Elementary') OR (JobTitle like '%Special%' OR JobTitle like 'Special %' OR JobTitle like '% Special') OR (JobTitle like '%University%' OR JobTitle like 'University %' OR JobTitle like '% University') OR (JobTitle like '%Lecturer%' OR JobTitle like 'Lecturer %' OR JobTitle like '% Lecturer') OR (JobTitle like '%tutor%' OR JobTitle like 'tutor %' OR JobTitle like '% tutor') OR (JobTitle like '%Adult%' OR JobTitle like 'Adult %' OR JobTitle like '% Adult') OR (JobTitle like '%Nursery%' OR JobTitle like 'Nursery %' OR JobTitle like '% Nursery') OR (JobTitle like '%Middle%' OR JobTitle like 'Middle %' OR JobTitle like '% Middle') OR (JobTitle like '%Multicultural%' OR JobTitle like 'Multicultural %' OR JobTitle like '% Multicultural') OR (JobTitle like '%Key Stage 1%' OR JobTitle like 'Key Stage 1 %' OR JobTitle like '% Key Stage 1') OR (JobTitle like '%Key Stage 2%' OR JobTitle like 'Key Stage 2 %' OR JobTitle like '% Key Stage 2') OR (JobTitle like '%KS1%' OR JobTitle like 'KS1 %' OR JobTitle like '% KS1') OR (JobTitle like '%KS2%' OR JobTitle like 'KS2 %' OR JobTitle like '% KS2') OR (JobTitle like '%KS 1%' OR JobTitle like 'KS 1 %' OR JobTitle like '% KS 1') OR (JobTitle like '%KS 2%' OR JobTitle like 'KS 2 %' OR JobTitle like '% KS 2') OR (JobTitle like '%Year 1%' OR JobTitle like 'Year 1 %' OR JobTitle like '% Year 1') OR (JobTitle like '%Year 2%' OR JobTitle like 'Year 2 %' OR JobTitle like '% Year 2') OR (JobTitle like '%Year 3%' OR JobTitle like 'Year 3 %' OR JobTitle like '% Year 3') OR (JobTitle like '%Year 4%' OR JobTitle like 'Year 4 %' OR JobTitle like '% Year 4') OR (JobTitle like '%Year 5%' OR JobTitle like 'Year 5 %' OR JobTitle like '% Year 5') OR (JobTitle like '%Year 6%' OR JobTitle like 'Year 6 %' OR JobTitle like '% Year 6') OR (JobTitle like '%social worker%' OR JobTitle like 'social worker %' OR JobTitle like '% social worker')) OR ((tblJobScrap.institutionName like '%Primary%' OR tblJobScrap.institutionName like 'Primary %' OR tblJobScrap.institutionName like '% Primary') OR (tblJobScrap.institutionName like '%Infant%' OR tblJobScrap.institutionName like 'Infant %' OR tblJobScrap.institutionName like '% Infant') OR (tblJobScrap.institutionName like '%Junior%' OR tblJobScrap.institutionName like 'Junior %' OR tblJobScrap.institutionName like '% Junior') OR (tblJobScrap.institutionName like '%Early%' OR tblJobScrap.institutionName like 'Early %' OR tblJobScrap.institutionName like '% Early') OR (tblJobScrap.institutionName like '%Elementary%' OR tblJobScrap.institutionName like 'Elementary %' OR tblJobScrap.institutionName like '% Elementary') OR (tblJobScrap.institutionName like '%Special%' OR tblJobScrap.institutionName like 'Special %' OR tblJobScrap.institutionName like '% Special') OR (tblJobScrap.institutionName like '%University%' OR tblJobScrap.institutionName like 'University %' OR tblJobScrap.institutionName like '% University') OR (tblJobScrap.institutionName like '%Lecturer%' OR tblJobScrap.institutionName like 'Lecturer %' OR tblJobScrap.institutionName like '% Lecturer') OR (tblJobScrap.institutionName like '%tutor%' OR tblJobScrap.institutionName like 'tutor %' OR tblJobScrap.institutionName like '% tutor') OR (tblJobScrap.institutionName like '%Adult%' OR tblJobScrap.institutionName like 'Adult %' OR tblJobScrap.institutionName like '% Adult') OR (tblJobScrap.institutionName like '%Nursery%' OR tblJobScrap.institutionName like 'Nursery %' OR tblJobScrap.institutionName like '% Nursery') OR (tblJobScrap.institutionName like '%Middle%' OR tblJobScrap.institutionName like 'Middle %' OR tblJobScrap.institutionName like '% Middle') OR (tblJobScrap.institutionName like '%Multicultural%' OR tblJobScrap.institutionName like 'Multicultural %' OR tblJobScrap.institutionName like '% Multicultural') OR (tblJobScrap.institutionName like '%Key Stage 1%' OR tblJobScrap.institutionName like 'Key Stage 1 %' OR tblJobScrap.institutionName like '% Key Stage 1') OR (tblJobScrap.institutionName like '%Key Stage 2%' OR tblJobScrap.institutionName like 'Key Stage 2 %' OR tblJobScrap.institutionName like '% Key Stage 2') OR (tblJobScrap.institutionName like '%KS1%' OR tblJobScrap.institutionName like 'KS1 %' OR tblJobScrap.institutionName like '% KS1') OR (tblJobScrap.institutionName like '%KS2%' OR tblJobScrap.institutionName like 'KS2 %' OR tblJobScrap.institutionName like '% KS2') OR (tblJobScrap.institutionName like '%KS 1%' OR tblJobScrap.institutionName like 'KS 1 %' OR tblJobScrap.institutionName like '% KS 1') OR (tblJobScrap.institutionName like '%KS 2%' OR tblJobScrap.institutionName like 'KS 2 %' OR tblJobScrap.institutionName like '% KS 2') OR (tblJobScrap.institutionName like '%Year 1%' OR tblJobScrap.institutionName like 'Year 1 %' OR tblJobScrap.institutionName like '% Year 1') OR (tblJobScrap.institutionName like '%Year 2%' OR tblJobScrap.institutionName like 'Year 2 %' OR tblJobScrap.institutionName like '% Year 2') OR (tblJobScrap.institutionName like '%Year 3%' OR tblJobScrap.institutionName like 'Year 3 %' OR tblJobScrap.institutionName like '% Year 3') OR (tblJobScrap.institutionName like '%Year 4%' OR tblJobScrap.institutionName like 'Year 4 %' OR tblJobScrap.institutionName like '% Year 4') OR (tblJobScrap.institutionName like '%Year 5%' OR tblJobScrap.institutionName like 'Year 5 %' OR tblJobScrap.institutionName like '% Year 5') OR (tblJobScrap.institutionName like '%Year 6%' OR tblJobScrap.institutionName like 'Year 6 %' OR tblJobScrap.institutionName like '% Year 6') OR (tblJobScrap.institutionName like '%social worker%' OR tblJobScrap.institutionName like 'social worker %' OR tblJobScrap.institutionName like '% social worker')))and (((JobTitle like '%Classroom Teacher%' OR JobTitle like 'Classroom Teacher %' OR JobTitle like '% Classroom Teacher') OR (JobTitle like '%teacher%' OR JobTitle like 'teacher %' OR JobTitle like '% teacher') OR (JobTitle like '%ordinator%' OR JobTitle like 'ordinator %' OR JobTitle like '% ordinator') OR (JobTitle like '%position%' OR JobTitle like 'position %' OR JobTitle like '% position'))) AND NOT (((JobTitle like '%Headteacher%' OR JobTitle like 'Headteacher %' OR JobTitle like '% Headteacher') OR (JobTitle like '%Head teacher%' OR JobTitle like 'Head teacher %' OR JobTitle like '% Head teacher') OR (JobTitle like '%Head%' OR JobTitle like 'Head %' OR JobTitle like '% Head') OR (JobTitle like '%lecturer%' OR JobTitle like 'lecturer %' OR JobTitle like '% lecturer') OR (JobTitle like '%assistant%' OR JobTitle like 'assistant %' OR JobTitle like '% assistant') OR (JobTitle like '%support%' OR JobTitle like 'support %' OR JobTitle like '% support')))and (((JobTitle like '%Science%' OR JobTitle like 'Science %' OR JobTitle like '% Science') OR (JobTitle like '%Chemistry%' OR JobTitle like 'Chemistry %' OR JobTitle like '% Chemistry') OR (JobTitle like '%Physics%' OR JobTitle like 'Physics %' OR JobTitle like '% Physics') OR (JobTitle like '%Biology%' OR JobTitle like 'Biology %' OR JobTitle like '% Biology')))AND (((LEA like '%Croydon%' OR LEA like 'Croydon %' OR LEA like '% Croydon'))) 
  4.  
While executing query like this the page load too much slow. While the other time the query is not to much lengthy but 10 20 or 50 number of queries are to be evaluated. Now the problem is that the site is too much slow for pages in which the above queries are to be evaluated. My client want to speed up the site. He is not happy with the speed of the site. Also he is not ready to change his requirements. If Any one has some recomondations.
Very very Thanks for sugessions and recomondations.
Mar 31 '09 #1
11 1633
gits
5,390 Expert Mod 4TB
wtf ;) ... that is quite a lot of conditions ;) ... as far as i see there are some joins and a union ... and those bunch of conditions ... so you could check whether the fields to search for are all indexed in the DB to avoid full table-scans and/or in case it would be possible you could create an 'aggregation'-table that contains the data ... what would be a kind of datawarehouse for this kind of queries to avoid the slow joins. so to say this is a DB-question ... may be the query could be optimized too.

kind regards
Mar 31 '09 #2
Frinavale
9,735 Expert Mod 8TB
I've moved this thread to the SQL Server forum. I think they'll have a better idea about how to help you with this. If you're not using SQL Server as your database, let me know and I'll move it accordingly.

-Moderator Frinny
Mar 31 '09 #3
ck9663
2,878 Expert 2GB
Some notes...

1. I assume you don't need all these fields immediately on the requesting page. So retrieve those you only need.
2. Create a view that will handle the UNION.
3. UNION vs UNION ALL. Union filters duplicate records which makes your entire query slow. UNION ALL returns all rows.
4. Create all the necessary index on your table


--- CK
Mar 31 '09 #4
Hamayun Khan
106 100+
@Frinavale
Thanks

I m using SQL Server 2005
Apr 1 '09 #5
Hamayun Khan
106 100+
@ck9663
Thanks for reply

1 I need All the fields I m currently retrieving.

2 I have already used Union All in my origional Query

3 I m not SQL Expert. But I m SQL Beginner. If you give me an example of how to create index on table for the given query.

Keep in mind each time the query is different. (means "Where" Clause is each time different)

Thanks
Apr 1 '09 #6
Are you repeating searches unnecessarily in the WHERE statement?

e.g.

Expand|Select|Wrap|Line Numbers
  1. WHERE (Region like '%London%' OR Region like 'London %' OR Region like '% London')

Are 'London %' and '% London' not subsets of '%London%' ?
I'm not sure if it will make any difference to the speed (CK can probably advise on that!)
Apr 1 '09 #7
Hamayun Khan
106 100+
@Uncle Dickie

Thanks

'%London %' does not fulfill my requirements.
Apr 1 '09 #8
gits
5,390 Expert Mod 4TB
but as above mentioned '%London%' should do ... ?

kind regards
Apr 1 '09 #9
ck9663
2,878 Expert 2GB
Here's the link to create index. Create an index on the columns that you mostly used for searching.

The dollar sign is a wild character. So your '%London%' can capture '% London' and 'London %' as well.


--- CK
Apr 1 '09 #10
Delerna
1,134 Expert 1GB
Just a side note, and I could be wrong

but I believe that using like on a field nullifies any benefit achieved by indexing that field.

The way I understand it is, an index gives speed improvements by reducing the number of records that the query must scan.
Using like forces the query to have to scan EVERY record (I believe) in order to find out whether the value in the field is like the criteria or not.

I am in no way criticising the use of LIKE here, just highlighting something that I believe needs to be carefully considered when using it.
Apr 2 '09 #11
ck9663
2,878 Expert 2GB
Delerna is right.

In some instance, SQL Server will use index if you use as much character on the right side of the string before the wildcard. A WHERE column like 'AAA%' will use an index, but a WHERE column like '%AAAA' will not.


--- CK
Apr 2 '09 #12

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

Similar topics

4
by: ChronoFish | last post by:
In PHP you can retrieve the table.column name of a MySQL query doing something like this: $result = mysql_query($query, $dbConnection); $resultArray = mysql_fetch_row($result); $i = 0;...
2
by: Bob | last post by:
Let me state up front that I know very little about XML. My experience is pretty much limited using the XML Serializer to serialize a user preferences class to a file and back again. I'm writing...
6
by: Eddie Smit | last post by:
field- field- surname town ---- ---- john NY john Vegas john Boston eddie Boston eddie New Orleans eddie NY
4
by: Theo Jansen | last post by:
Hi, i'm making an application in Access and in the query made, i'd like the user to fill in a parameter when opening the query (in a report). It's much easier for the user if the parameter was a...
21
by: mollyf | last post by:
I'm creating a query, which I want to use in code in my VB.NET app. This query produces the correct results when executed in Access: SELECT tblEncounters.EncounterBeginDT, Query11.RID,...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
3
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
3
by: paulquinlan100 | last post by:
Hi I'm trying to tweak the query below so that the second SELECT only pulls out one record per "Site_Ref", i.e. the record that has the maximum "appraisal_date" out of the 4 Appraisal_types. Is...
9
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.