Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server 2005 Index

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

618 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