Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SP - Efficient "Find" method

SP - Efficient "Find" method

Scheduled Pinned Locked Moved Database
sharepointdatabaseperformancecode-review
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UB
    wrote on last edited by
    #1

    I want another efficient way to re-write this query. Note : @empid, @batchid and @name are not always passed to the SP

    CREATE PROCEDURE FindEmployee
    @empid INTEGER = NULL,
    @batchid INTEGER = NULL,
    @name VARCHAR(30) = '%'
    AS

    SELECT \* FROM Employees
    WHERE  @empid LIKE COALESCE(CONVERT(varchar(3), @empid), '%')
    AND    @batchid LIKE COALESCE(CONVERT(varchar(3), @batchid), '%')
    AND    name LIKE ('%' + @name + '%'
    

    1. If @empid and @batchid were not provided, is there a way to remove thoese columns from the where clause conditionally without writing a seperate query 2. What are the otherways to improve the performance of this kind of a query Thanks UB You may stop this individual, but you can't stop us all... after all, we're all alike. +++Mentor+++

    D 1 Reply Last reply
    0
    • U UB

      I want another efficient way to re-write this query. Note : @empid, @batchid and @name are not always passed to the SP

      CREATE PROCEDURE FindEmployee
      @empid INTEGER = NULL,
      @batchid INTEGER = NULL,
      @name VARCHAR(30) = '%'
      AS

      SELECT \* FROM Employees
      WHERE  @empid LIKE COALESCE(CONVERT(varchar(3), @empid), '%')
      AND    @batchid LIKE COALESCE(CONVERT(varchar(3), @batchid), '%')
      AND    name LIKE ('%' + @name + '%'
      

      1. If @empid and @batchid were not provided, is there a way to remove thoese columns from the where clause conditionally without writing a seperate query 2. What are the otherways to improve the performance of this kind of a query Thanks UB You may stop this individual, but you can't stop us all... after all, we're all alike. +++Mentor+++

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      As a general rule, don't use functions or the LIKE operator on your table columns or your query won't use any indexes. The easiest way of speeding this query is by replacing this (I assume there's a typo and there is no '@' outside the COALESCE):

      WHERE empid LIKE COALESCE(CONVERT(varchar(3), @empid), '%')
      AND batchid LIKE COALESCE(CONVERT(varchar(3), @batchid), '%')

      for something like this:

      WHERE empid BETWEEN @firstEmpid AND @lastEmpid
      AND batchid BETWEEN @firstBatchid AND @lastBatchid

      And do a bit of math to calculate the first and last ids. If this filter returns a small result set, then the slowest part (show below) of the query won't need to run against a lot of records and the query will be way faster.

      AND name LIKE ('%' + @name + '%'

      Yes, even I am blogging now!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups