Link to home
Create AccountLog in
Microsoft SQL Server 2005

Microsoft SQL Server 2005

--

Questions

--

Followers

Top Experts

Avatar of Senz79
Senz79🇮🇳

Optimize SQL Query
Hello All

I have a query below and i need to optimize and generalize it .
The like parameters are subject to change as per the user select the domain. Please help

Senz
Select distinct SourceDomainName from ITM_Alerts where 
SourceDomainName like '%ABC%' and SourceDomainName like '%DEF%' and SourceDomainName like '%HIJ%'
and SourceDomainName like '%KLO%'

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Senz79Senz79🇮🇳

ASKER

Just anather input
The query fetches around 100 rows as all the domains have sub domains, and in the current senario its taking much time.

Avatar of Kevin CrossKevin Cross🇺🇸

Do you have an index on SourceDomainName ? And it is curious that you are using AND and not OR for the differing LIKE statements ... begs the question if you couldn't just put them in the pattern which they should appear in one like.

LIKE '%abc%def%ghi%jkl%'

Or something to that effect.

Avatar of Senz79Senz79🇮🇳

ASKER

If you ar espeaking about the pattern

Select distinct SourceDomainName from ITM_Alerts where
SourceDomainName like '%ABC%DEF%HIJ%KLO%'
this is not possible as the like parameters are diffrent values and not part of a single string.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Kevin CrossKevin Cross🇺🇸

Then HOW can you use an AND??

select distinct SourceDomainName
from ITM_Alerts
where SourceDomainName like '%ABC%'
   and SourceDomainName like '%DEF%'
   and SourceDomainName like '%HIJ%'
   and SourceDomainName like '%KLO%'

Should probably be:

select distinct SourceDomainName
from ITM_Alerts
where SourceDomainName like '%ABC%'
      or SourceDomainName like '%DEF%'
      or SourceDomainName like '%HIJ%'
      or SourceDomainName like '%KLO%'

You may not need this unless you have duplicates in the data. Just noting in case you were doing this in case a domain matches multiple.

Avatar of Senz79Senz79🇮🇳

ASKER

The query tries to find all sub domains for the given LIKE parameters i have to used AND because i want all the sub domains for the parent domains.

Avatar of Senz79Senz79🇮🇳

ASKER

select distinct SourceDomainName
from ITM_Alerts
where SourceDomainName like '%CSC%'
      or SourceDomainName like '%BHP%'
      or SourceDomainName like '%IMM%'
      or SourceDomainName like '%RIO%'
this will also return the same result , but can we optimize the time

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Kevin CrossKevin Cross🇺🇸

Okay what about my index question, is there one?

Avatar of Senz79Senz79🇮🇳

ASKER

No there is no Index, can i implement one, kindly tell me how to do the same

ASKER CERTIFIED SOLUTION
Avatar of Kevin CrossKevin Cross🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Senz79Senz79🇮🇳

ASKER

solution was fine but didnt had the expected result

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Microsoft SQL Server 2005

Microsoft SQL Server 2005

--

Questions

--

Followers

Top Experts

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.