Solved

SQL Server 2005 Index

Posted on 2010-09-09
5
260 Views
Last Modified: 2012-05-10
We have a table that contains address information.  It has a primary key, called building code, and then the country, state, city, street and building name.  I'm using this table to populate a series of drop down menus dynamically when a user selects the parent drop down.  So if they select country, states are populated, selects state, cities are populated, etc.  When a user selects a country, the state, city, and street queries run pretty quickly but it takes almost 15 seconds to load building name.  I'm trying to figure out how to properly index this table to optimize the query.  We're planning on breaking the table out into separate entities but I'm just trying to see if there's a quick fix for the time being.

Here's the query I'm using
DECLARE @searchType            VARCHAR(15)
DECLARE @country            VARCHAR(100)
DECLARE @state                  VARCHAR(100)
DECLARE @city                  VARCHAR(100)
DECLARE @street                  VARCHAR(100)

SET @searchType = 'building'
SET @Country = 'UNITED STATES OF AMERICA'
SET @State = 'All'
SET @city = 'All'
SET @street = 'All'


SELECT DISTINCT
      BC.BuildingCode,
      BC.BuildingName
FROM        
      t_ResourceLocationAssignment AS RLA INNER JOIN
      t_CorporateDirectory_BuildingCode AS BC ON LEFT(RLA.mailCode,7) = BC.BuildingCode
WHERE
      BC.Country = (CASE WHEN @Country IN('All','') THEN BC.Country ELSE @Country END)
AND
      BC.State = (CASE WHEN @state IN('All','') THEN BC.State ELSE @State END)
AND
      BC.City = (CASE WHEN @city IN('All','') THEN BC.City ELSE @City END)
AND
      BC.StreetAddress = (CASE WHEN @street IN('All','') THEN BC.StreetAddress ELSE @street END)
AND
      BC.BuildingName <> ''
ORDER BY
      BC.BuildingName
0
Comment
Question by:stretch73
  • 2
  • 2
5 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33636883
Since you have the below statement in your query

AND BC.BuildingName <> ''
ORDER BY BC.BuildingName

To increase performance of ORDER BY clause, creating an index on BuildingName would improve the performance a little bit.
But checking BuildingName <> '' would force a table scan thereby reducing the performance and inefficient usage of indexes..
0
 

Author Comment

by:stretch73
ID: 33636912
I have an index on building name.  I'd really like to bring back records that don't have a ' ' value, is there a more efficient way of doing that?
0
 
LVL 11

Accepted Solution

by:
Larissa T earned 500 total points
ID: 33637294
I would suggest replacing all "case" with "isnull"

BC.Country = (CASE WHEN @Country IN('All','') THEN BC.Country ELSE @Country END)

--- redefine all variables as
if @Country in ('All','')  set @Country=null
-- then use
where BC.Country= isnull(@Country,BC.Country)

and instead of
 AND BC.BuildingName <> ''
use
AND BC.BuildingName  > ''
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33637313
>> I'd really like to bring back records that don't have a ' ' value, is there a more efficient way of doing that?

Nope, this search condition ideally would go for a table scan.

>> LEFT(RLA.mailCode,7) = BC.BuildingCode

Above condition would also force a table scan.
And both scenarios can be handled only by performing change in database design.
0
 

Author Closing Comment

by:stretch73
ID: 33638258
I had to add a LEN() to this:

AND BC.BuildingName  > ''

But this answer got me a significant performance improvement.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now