?
Solved

SQL Server 2005 Index

Posted on 2010-09-09
5
Medium Priority
?
268 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 2000 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

777 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