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

x
?
Solved

MSSQL/TSQL Stored Procedure needs optimization, any ideas?

Posted on 2004-10-06
8
Medium Priority
?
401 Views
Last Modified: 2006-11-17
I'm looking for some pointers here on how to optimize this stored procedure and how to measure it's performance. This is going to be used on a site that supports multiple languages.
It involves two tables. Genres and GenresText. GenresText is a table with all the localized strings in it with a GenreID in it referring back to Genres.

If all text was populated a normal query would be:
SELECT *
FROM Genres,GenresText
WHERE Genres.GenreID = GenresText.GenreID
 AND GenresText.LocaleID = @localeID
 AND Genres.StatusID = 1
 AND GenresText.StatusID = 1

But that's not the case, I've created a fallback algorithm that will look for text from the preferred locale and if that doesn't exist then get it from the fallback locale. That algorithm is implemented in the stored procedure you will find below.

Genres Table:
CREATE TABLE [dbo].[Genres] ( [GenreID] [int] IDENTITY (1, 1) NOT NULL , [StatusID] [int] NOT NULL ) ON [PRIMARY]
ALTER TABLE [dbo].[Genres] WITH NOCHECK ADD  CONSTRAINT [PK_Genres] PRIMARY KEY  CLUSTERED ( [GenreID] )  ON [PRIMARY]

GenresText Table:
CREATE TABLE [dbo].[GenresText] (
      [GenreTextID] [int] IDENTITY (1, 1) NOT NULL ,
      [GenreID] [int] NOT NULL ,
      [LocaleID] [int] NOT NULL ,
      [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [StatusID] [int] NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[GenresText] WITH NOCHECK ADD CONSTRAINT [PK_GenresText] PRIMARY KEY  CLUSTERED ([GenreTextID])  ON [PRIMARY]

GetArtistGenres Stored Procedure:
ALTER PROCEDURE getArtistGenres
@localeID int,
@fallBackLocaleID int,
@showStaging int
AS

SET NOCOUNT ON

CREATE TABLE #tempGenres (GID INT, Title VARCHAR(255))

DECLARE @title VARCHAR(255)
DECLARE @productStatus VARCHAR(255)
 
 
INSERT #tempGenres (GID)
SELECT GenreID FROM Genres

DECLARE genres_cursor INSENSITIVE CURSOR
FOR
      SELECT GID FROM #tempGenres

OPEN genres_cursor
DECLARE @tgid int
FETCH genres_cursor INTO @tgid

WHILE @@FETCH_STATUS = 0
BEGIN
      IF EXISTS (SELECT Title FROM GenresText,Genres WHERE Genres.GenreID = GenresText.GenreID AND GenresText.LocaleID in (@localeID,@fallbackLocaleID) AND GenresText.GenreID=@tgid)
      BEGIN
            IF @showStaging = 1
                  BEGIN
                        SELECT @title = GenresText.Title
                        FROM GenresText INNER JOIN
                              Genres ON GenresText.GenreID = Genres.GenreID
                        WHERE (GenresText.LocaleID = @localeID)
                              AND (GenresText.GenreID = @tgid)
                              AND (Genres.StatusID = 1)
                              AND (GenresText.StatusID = 1)
                  END
            ELSE
                  BEGIN
                        SELECT @title = GenresText.Title
                        FROM GenresText INNER JOIN
                              Genres ON GenresText.GenreID = Genres.GenreID
                        WHERE (GenresText.LocaleID = @localeID)
                              AND (GenresText.GenreID = @tgid)
                              AND (Genres.StatusID in (1,2))
                              AND (GenresText.StatusID in (1,2))
                  END
            END
      ELSE
            IF @showStaging = 1
                  BEGIN
                        SELECT @title = GenresText.Title
                        FROM GenresText INNER JOIN
                              Genres ON GenresText.GenreID = Genres.GenreID
                        WHERE (GenresText.LocaleID = @fallBackLocaleID)
                              AND (GenresText.GenreID = @tgid)
                              AND (Genres.StatusID = 1)
                              AND (GenresText.StatusID = 1)
                  END
            ELSE
                        SELECT @title = GenresText.Title
                        FROM GenresText INNER JOIN
                              Genres ON GenresText.GenreID = Genres.GenreID
                        WHERE (GenresText.LocaleID = @fallBackLocaleID)
                              AND (GenresText.GenreID = @tgid)
                              AND (Genres.StatusID in (1,2))
                              AND (GenresText.StatusID in (1,2))
            
            UPDATE #tempGenres
                  SET title = @title
            WHERE GID=@tgid
            
      FETCH genres_cursor INTO @tgid
END

SET NOCOUNT OFF

SELECT GID,Title
FROM #tempGenres
ORDER BY Title

-- # LEAVE AT END #
DROP TABLE #tempGenres  
CLOSE genres_cursor  
DEALLOCATE genres_cursor
-- # LEAVE AT END #



0
Comment
Question by:jbrahy
[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
  • 4
  • 4
8 Comments
 
LVL 7

Accepted Solution

by:
Nievergelt earned 1000 total points
ID: 12241843
If you are looking to boost performance:
- Do not use cursors. They are notoriously slow as they do not allow the database engine do help make things faster.
- Try to use joins (even outer joins), the COALESCE function and maybe also CASE clauses.

Try something like this:

SELECT COALESCE (loc.title, altLoc.title) AS title
FROM Genres g INNER JOIN GenresText altLoc ON g.genreId = altLoc.genreId AND altLoc.localeId = 1
                       LEFT OUTER JOIN GenresText loc ON altLoc.genreId = loc.genreId AND loc.localeId = 2
0
 
LVL 1

Author Comment

by:jbrahy
ID: 12242034
damn, that's good.

I've never used COALESCE before, I really like it now.

SELECT COALESCE (loc.title, altLoc.title) AS title
FROM Genres
        LEFT OUTER JOIN GenresText loc
            ON Genres.GenreID = loc.GenreID
                  AND loc.LocaleID= 2
      INNER JOIN GenresText altLoc
            ON Genres.GenreID = altLoc.GenreID
                  AND altLoc.LocaleID = 1

I usually do my joins without the JOIN keywords, can this be done in that fashion?
something like this?

SELECT COALESCE (loc.title, altLoc.title) AS title
FROM Genres, GenresText loc altloc
WHERE Genres.GenreID = loc.GenreID AND loc.LocaleID= 2
  AND Genres.GenreID = altLoc.GenreID AND altLoc.LocaleID = 1





0
 
LVL 7

Expert Comment

by:Nievergelt
ID: 12242207
If you do not use the JOIN syntax, I think you can only do inner joins.
But what you want to do is to have an outer join, i.e. a join where the columns of the missing table row are filled with "virtual" NULL values to be able to recognize the missing values.
Instead of COALESCE (that works also for multiple values) the same result could also be achieved with an appropriate CASE WHEN ... IS NULL ...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:jbrahy
ID: 12242334
Which is more efficent? and how do I check the process utilization?
0
 
LVL 7

Expert Comment

by:Nievergelt
ID: 12242402
I do not assume that you will see performance differences; it is just a matter of style.
0
 
LVL 1

Author Comment

by:jbrahy
ID: 12242495
The original question had two parts, what is a way to optimize the code and how can I measure the optimizations. Do you have an answer to that half of the question?

Thanks,

John
0
 
LVL 7

Expert Comment

by:Nievergelt
ID: 12242642
Sorry John,

I do not know how to measure perfomance with SQL Server.

Maybe you start a new question?

Cheers  Christoph
0
 
LVL 1

Author Comment

by:jbrahy
ID: 12242717
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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