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. alternative to rewire CASE WHEN in query

alternative to rewire CASE WHEN in query

Scheduled Pinned Locked Moved Database
databasequestion
9 Posts 5 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.
  • P Offline
    P Offline
    prachidalwadi
    wrote on last edited by
    #1

    Hello there... I have a query which spends a lot of time calculating my CASE WHEN -statements. There are around 16 CASE WHEN statements, which takes a huge time to calculate. Is there any way to rewrite query? My query looks like this SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate, CASE WHEN (Product_Master.Product_Code = '' OR Product_Master.Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END AS Product_Code, CASE WHEN (Product_Master.Product_ModelNo = '' OR Product_Master.Product_ModelNo IS NULL) THEN '-' ELSE Product_Master.Product_ModelNo END AS Product_ModelNo, CASE WHEN (Product_Master.Product_Image = '' OR Product_Master.Product_Image IS NULL) THEN 'N' ELSE 'Y' END AS Product_Image, CASE WHEN (Product_Master.Product_BrandId = '' OR Product_Master.Product_BrandId IS NULL) THEN 'N' ELSE 'Y' END AS Brand_Name FROM Product_Master ORDER BY RegDate DESC

    H S P J 4 Replies Last reply
    0
    • P prachidalwadi

      Hello there... I have a query which spends a lot of time calculating my CASE WHEN -statements. There are around 16 CASE WHEN statements, which takes a huge time to calculate. Is there any way to rewrite query? My query looks like this SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate, CASE WHEN (Product_Master.Product_Code = '' OR Product_Master.Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END AS Product_Code, CASE WHEN (Product_Master.Product_ModelNo = '' OR Product_Master.Product_ModelNo IS NULL) THEN '-' ELSE Product_Master.Product_ModelNo END AS Product_ModelNo, CASE WHEN (Product_Master.Product_Image = '' OR Product_Master.Product_Image IS NULL) THEN 'N' ELSE 'Y' END AS Product_Image, CASE WHEN (Product_Master.Product_BrandId = '' OR Product_Master.Product_BrandId IS NULL) THEN 'N' ELSE 'Y' END AS Brand_Name FROM Product_Master ORDER BY RegDate DESC

      H Offline
      H Offline
      Herman T Instance
      wrote on last edited by
      #2

      case when (isnull(Product_Master.Product_Code, '') = '' then '-' else Product_Master.Product_Code end as Product_Code

      In Word you can only store 2 bytes. That is why I use Writer.

      1 Reply Last reply
      0
      • P prachidalwadi

        Hello there... I have a query which spends a lot of time calculating my CASE WHEN -statements. There are around 16 CASE WHEN statements, which takes a huge time to calculate. Is there any way to rewrite query? My query looks like this SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate, CASE WHEN (Product_Master.Product_Code = '' OR Product_Master.Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END AS Product_Code, CASE WHEN (Product_Master.Product_ModelNo = '' OR Product_Master.Product_ModelNo IS NULL) THEN '-' ELSE Product_Master.Product_ModelNo END AS Product_ModelNo, CASE WHEN (Product_Master.Product_Image = '' OR Product_Master.Product_Image IS NULL) THEN 'N' ELSE 'Y' END AS Product_Image, CASE WHEN (Product_Master.Product_BrandId = '' OR Product_Master.Product_BrandId IS NULL) THEN 'N' ELSE 'Y' END AS Brand_Name FROM Product_Master ORDER BY RegDate DESC

        S Offline
        S Offline
        scottgp
        wrote on last edited by
        #3

        Personally, I find it hard to believe that the CASE statements are what's taking time since they don't look complex. How do you know that the CASE statements are what take the most time? Have you tried the same query without some of the CASE statements, just returning the actual column data instead, and comparing the performance? Is the DISTINCT required? Is RegDate indexed? Scott

        P 1 Reply Last reply
        0
        • P prachidalwadi

          Hello there... I have a query which spends a lot of time calculating my CASE WHEN -statements. There are around 16 CASE WHEN statements, which takes a huge time to calculate. Is there any way to rewrite query? My query looks like this SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate, CASE WHEN (Product_Master.Product_Code = '' OR Product_Master.Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END AS Product_Code, CASE WHEN (Product_Master.Product_ModelNo = '' OR Product_Master.Product_ModelNo IS NULL) THEN '-' ELSE Product_Master.Product_ModelNo END AS Product_ModelNo, CASE WHEN (Product_Master.Product_Image = '' OR Product_Master.Product_Image IS NULL) THEN 'N' ELSE 'Y' END AS Product_Image, CASE WHEN (Product_Master.Product_BrandId = '' OR Product_Master.Product_BrandId IS NULL) THEN 'N' ELSE 'Y' END AS Brand_Name FROM Product_Master ORDER BY RegDate DESC

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Yeah, what they say. Plus why convert (and transmit) the textual RegDate? Perform that outside the database.

          1 Reply Last reply
          0
          • S scottgp

            Personally, I find it hard to believe that the CASE statements are what's taking time since they don't look complex. How do you know that the CASE statements are what take the most time? Have you tried the same query without some of the CASE statements, just returning the actual column data instead, and comparing the performance? Is the DISTINCT required? Is RegDate indexed? Scott

            P Offline
            P Offline
            prachidalwadi
            wrote on last edited by
            #5

            Thanks for reply Scott, Yes, I have tried the same query without CASE WHEN statements, and it works faster comperatively. Moreover, The query is not this much only, there are many more CASE WHEN and Left Joins(which are necessary). Left Joins are essential for the result, so I can't move it. The only options is to find alternate for CASE WHEN statements. Prachi

            S H 2 Replies Last reply
            0
            • P prachidalwadi

              Thanks for reply Scott, Yes, I have tried the same query without CASE WHEN statements, and it works faster comperatively. Moreover, The query is not this much only, there are many more CASE WHEN and Left Joins(which are necessary). Left Joins are essential for the result, so I can't move it. The only options is to find alternate for CASE WHEN statements. Prachi

              S Offline
              S Offline
              scottgp
              wrote on last edited by
              #6

              Then we'd probably have to see the whole query since the little we've seen doesn't indicate a problem. It would help to know the table structures and rough row counts. Scott

              P 1 Reply Last reply
              0
              • P prachidalwadi

                Hello there... I have a query which spends a lot of time calculating my CASE WHEN -statements. There are around 16 CASE WHEN statements, which takes a huge time to calculate. Is there any way to rewrite query? My query looks like this SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate, CASE WHEN (Product_Master.Product_Code = '' OR Product_Master.Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END AS Product_Code, CASE WHEN (Product_Master.Product_ModelNo = '' OR Product_Master.Product_ModelNo IS NULL) THEN '-' ELSE Product_Master.Product_ModelNo END AS Product_ModelNo, CASE WHEN (Product_Master.Product_Image = '' OR Product_Master.Product_Image IS NULL) THEN 'N' ELSE 'Y' END AS Product_Image, CASE WHEN (Product_Master.Product_BrandId = '' OR Product_Master.Product_BrandId IS NULL) THEN 'N' ELSE 'Y' END AS Brand_Name FROM Product_Master ORDER BY RegDate DESC

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                I suppose that the nulls are there because of the left joins, you refer to in your answer to Scott, and therefore is nothing you can do anything about. But how about the zero length strings, do they serve a purpose? If not, you can add a check to the columns that disallows zero length strings at inserts and then you can remove that check in the query, which should speed it up a bit. Alternatively swap to Oracle where you can use a Function Based Index which would speed up things immensely. Like: Create index PM_PRODUCTCODE_EXIST_IDX ON PRODUCT_MASTER(Product_Kid,CASE WHEN (Product_Code = '' OR Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END);

                "When did ignorance become a point of view" - Dilbert

                1 Reply Last reply
                0
                • P prachidalwadi

                  Thanks for reply Scott, Yes, I have tried the same query without CASE WHEN statements, and it works faster comperatively. Moreover, The query is not this much only, there are many more CASE WHEN and Left Joins(which are necessary). Left Joins are essential for the result, so I can't move it. The only options is to find alternate for CASE WHEN statements. Prachi

                  H Offline
                  H Offline
                  Herman T Instance
                  wrote on last edited by
                  #8

                  Left joins..... yup that's costly

                  In Word you can only store 2 bytes. That is why I use Writer.

                  1 Reply Last reply
                  0
                  • S scottgp

                    Then we'd probably have to see the whole query since the little we've seen doesn't indicate a problem. It would help to know the table structures and rough row counts. Scott

                    P Offline
                    P Offline
                    prachidalwadi
                    wrote on last edited by
                    #9

                    Scott, here is the whole query. and roughly, records for each supplier is minimum 400(rows) declare @a as char(1) declare @b as varchar(35) set @a='Y' set @b='<span style=''color:Red''>N</span>' SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert(nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate, Isnull(Product_Master.Product_Code,'-') AS Product_Code, Isnull(Product_Master.Product_ModelNo,'-') AS Product_ModelNo, CASE WHEN (Product_Master.Product_Image IS NULL) THEN @b ELSE @a END AS Product_Image, CASE WHEN (Product_Master.Product_BrandId IS NULL) THEN @b ELSE @a END AS Brand_Name, CASE WHEN (Product_Master.Product_MarketPrice IS NULL) THEN @b ELSE @a END AS Product_MarketPrice, CASE WHEN (Product_Master.Product_PackagingCharge IS NULL) THEN @b ELSE @a END AS Product_PackagingCharge, CASE WHEN (Product_Master.Product_MinOrderQty IS NULL) THEN @b ELSE @a END AS Product_MinOrderQty, CASE WHEN (Product_Master.Product_ShippingQty IS NULL) THEN @b ELSE @a END AS Product_ShippingQty, CASE WHEN (Product_Master.Product_Weight IS NULL) THEN @b ELSE @a END AS Product_Weight, CASE WHEN (Product_Master.Product_Dimension IS NULL) THEN @b ELSE @a END AS Product_Dimension, CASE WHEN (Product_Master.Product_DeliveryPeriod IS NULL) THEN @b ELSE @a END AS Product_DeliveryPeriod, CASE WHEN (Product_Master.Product_Description Is NULL) THEN @b ELSE @a END AS Product_Description, CASE WHEN (Product_Master.Product_PcsPerKg IS NULL) THEN @b ELSE @a END AS Product_PcsPerKg, CASE WHEN (ProductApplication.ProdApp_Code IS NULL) THEN @b ELSE @a END AS ProdApp_Code, CASE WHEN (ProductFeatures.ProdFeatures_Code IS NULL) THEN @b ELSE @a END AS ProdFeatures_Code, CASE WHEN (ProductTechnicalSpecification.TechSpec_code IS NULL) THEN @b ELSE @a END AS TechSpec_code, CASE WHEN (ProductSpecialNotesInstruction.ProdSpecInstr_Code IS NULL) THEN @b ELSE @a END AS ProdSpecInstr_Code, CASE WHEN (MaterialSafetyDataSheet.MSDS_Code IS NULL) THEN @b ELSE @a END AS MSDS_Code, CASE WHEN (ProductStandardApproval.ProdStdApproval_Code IS NULL) THEN @b ELSE @a END AS ProdStdApproval_Code, CASE WHEN (InstallationManual.InstCommManual_Code IS NULL) THEN @b ELSE @a END AS InstCommManual_Code, CASE WHEN (ProductPackaging.ProdPackaging_Code IS NULL) THEN @b ELSE @a END AS Product_Pack

                    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