• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

MSSQL/TSQL Stored Procedure needs optimization, any ideas?

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
jbrahy
Asked:
jbrahy
  • 4
  • 4
1 Solution
 
NievergeltSenior SW DevCommented:
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
 
jbrahyAuthor Commented:
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
 
NievergeltSenior SW DevCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
jbrahyAuthor Commented:
Which is more efficent? and how do I check the process utilization?
0
 
NievergeltSenior SW DevCommented:
I do not assume that you will see performance differences; it is just a matter of style.
0
 
jbrahyAuthor Commented:
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
 
NievergeltSenior SW DevCommented:
Sorry John,

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

Maybe you start a new question?

Cheers  Christoph
0
 
jbrahyAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now