Solved

SQL Server 2005 Index

Posted on 2010-09-09
5
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 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