Solved

SQL Server 2005 Index

Posted on 2010-09-09
5
265 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

730 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