Avoid using functions in where clauses as it causes performance issues?


Vikaskubal

Whenever I execute a program, I have performance issues, where do I need to change the program to improve performance?

I am calling a table function in a where clause and I need to optimize this process without using strings.

CREATE PROC proc_productwise_report @cmp_id VARCHAR(max), @unitcode VARCHAR(max), @gr_code VARCHAR(max), @store_code VARCHAR(max), @from_dt VARCHAR(20), @to_dt VARCHAR(20)
AS
BEGIN
    SELECT sh.cmp_id, d.unitcode, d.store_code, st.item_code AS product, d.item_code, im.item_desc, SUM(charge_qty) AS challan_qty
    FROM ps_invenstatic sh
    INNER JOIN ps_invenstaticdet st ON sh.cmp_id = st.cmp_id
        AND sh.sys_no_id = st.sys_no_id
        AND sh.doc_id = st.doc_id
        AND sys_doc_type = 'PSCH'
    INNER JOIN ps_invenissu h ON sh.cmp_id = h.cmp_id
        AND sh.doc_type = h.ref_doc_type
        AND sh.doc_no = h.ref_doc_no
        AND h.prod_code = st.item_code
    INNER JOIN ps_invenissudet d ON h.cmp_id = d.cmp_id
        AND h.sys_no_id = d.sys_no_id
        AND h.doc_id = d.doc_id
    INNER JOIN ps_itemmas im ON sh.cmp_id = im.cmp_id
        AND im.item_code = d.item_code
    WHERE sh.cmp_id IN (
            SELECT *
            FROM utilfn_split(@cmp_id, ',')
            )
        AND d.unitcode IN (
            SELECT *
            FROM utilfn_split(@unitcode, ',')
            )
        AND im.gr_code IN (
            SELECT *
            FROM utilfn_split(@gr_code, ',')
            )
        AND d.store_code IN (
            SELECT *
            FROM utilfn_split(@store_code, ',')
            )
        AND h.doc_dt BETWEEN convert(DATETIME, @from_dt, 103)
            AND convert(DATETIME, @to_dt, 103)
        AND sh.Stat_Code <> 'CA'
    GROUP BY sh.cmp_id, d.unitcode, d.store_code, st.item_code, d.item_code, im.item_desc
END

I need to avoid using functions in where clauses and solve performance issues.

Radu Georgius

You can use the results of SPLIT and INNER JOIN in the main query to build temporary tables in stored procedures.

CREATE PROC proc_productwise_report @cmp_id VARCHAR(max), @unitcode VARCHAR(max), 
@gr_code VARCHAR(max), @store_code VARCHAR(max), @from_dt VARCHAR(20), @to_dt VARCHAR(20)
AS
BEGIN

SELECT *
INTO #cmp_ids
FROM utilfn_split(@cmp_id, ',');

SELECT *
INTO #unitcodes
FROM utilfn_split(@unitcode, ',');

SELECT *
INTO #gr_codes
FROM utilfn_split(@gr_code, ',');

SELECT *
INTO #store_codes
FROM utilfn_split(@store_code, ',');

SELECT 
    sh.cmp_id
    , d.unitcode
    , d.store_code
    , st.item_code AS product
    , d.item_code
    , im.item_desc
    , SUM(charge_qty) AS challan_qty
FROM ps_invenstatic sh
    INNER JOIN ps_invenstaticdet st 
        ON sh.cmp_id = st.cmp_id
            AND sh.sys_no_id = st.sys_no_id
            AND sh.doc_id = st.doc_id
            AND sys_doc_type = 'PSCH'
    INNER JOIN ps_invenissu h 
        ON sh.cmp_id = h.cmp_id
            AND sh.doc_type = h.ref_doc_type
            AND sh.doc_no = h.ref_doc_no
            AND h.prod_code = st.item_code
    INNER JOIN ps_invenissudet d 
        ON h.cmp_id = d.cmp_id
            AND h.sys_no_id = d.sys_no_id
            AND h.doc_id = d.doc_id
    INNER JOIN ps_itemmas im 
        ON sh.cmp_id = im.cmp_id
            AND im.item_code = d.item_code
    INNER JOIN #cmp_ids tci on sh.cmp_id = tci.[value]
    INNER JOIN #unitcodes tuc on d.unitcode = tuc.[value]
    INNER JOIN #gr_codes tgr on im.gr_code = tgr.[value]
    INNER JOIN #store_codes tsc on d.store_code = tsc.[value]
WHERE h.doc_dt BETWEEN convert(DATETIME, @from_dt, 103)
    AND convert(DATETIME, @to_dt, 103)
    AND sh.Stat_Code <> 'CA'
GROUP BY sh.cmp_id
    , d.unitcode
    , d.store_code
    , st.item_code
    , d.item_code
    , im.item_desc
END 

Related


Performance of using 2 where clauses in LINQ

rexcfnghk In LINQ-to-Entities, you can query entities in the following ways: var students = SchoolContext.Students.Where(s => s.Name == "Foo" && s.Id == 1); I know that behind the scenes it will be converted to SQL similar to the following: SELECT * FROM Stud

Performance with many where clauses

Deepa Vasanthkumar I have a hive query select * from tbl where month(dt) = '06' and year(dt) = '2011' I would like to ask if it will be any good if I join the club every year and add new udfs to it, select * from tbl where yearMonth(dt) = '201106' I'm new to H

SQL; functions and where clauses

Staba I'm trying to find out which country's population is closest to the global average country population. I have the following relationship: Column | Type | Modifiers ------------+-----------------------+----------- name | char

SQL; functions and where clauses

Staba I'm trying to find out which country's population is closest to the global average country population. I have the following relationship: Column | Type | Modifiers ------------+-----------------------+----------- name | char

Functions in where clauses - BQ

Maria I've always avoided using functions in WHERE or ON clauses. However, I'm using Big Query now, and I'm wondering if it's the same as the data warehouse in the "old" data warehouse, since it doesn't have indexes. I still avoid it, but I'm doing a code revi

Group functions in where IN clauses

Imran Hermani I have two tables with seq_no column: select max(SEQ_NO) from final_prices; select max(SEQ_NO) from price_loads; I want to insert into third table: archive_load from final_prices table with condition if MAX(SEQ_NO) in final_prices exists in pric

SQL; functions and where clauses

Staba I'm trying to find out which country's population is closest to the global average country population. I have the following relationship: Column | Type | Modifiers ------------+-----------------------+----------- name | char

SQL IN clause where condition causes big data performance issues

username The following query is working fine in my database but creating huge performance issues in the customer database. I know that I am using IN clause in where condition which is giving me this problem. But I don't know how to fix it. declare @AccountId i

SQL IN clause where condition causes big data performance issues

username The following query is working fine in my database but creating huge performance issues in the customer database. I know that I am using IN clause in where condition which is giving me this problem. But I don't know how to fix it. declare @AccountId i

SQL IN clause where condition causes big data performance issues

username The following query is working fine in my database but creating huge performance issues in the customer database. I know that I am using IN clause in where condition which is giving me this problem. But I don't know how to fix it. declare @AccountId i

SQL IN clause where condition causes big data performance issues

username The following query is working fine in my database but creating huge performance issues in the customer database. I know that I am using IN clause in where condition which is giving me this problem. But I don't know how to fix it. declare @AccountId i

SQL IN clause where condition causes big data performance issues

username The following query is working fine in my database but creating huge performance issues in the customer database. I know that I am using IN clause in where condition which is giving me this problem. But I don't know how to fix it. declare @AccountId i

SQL IN clause where condition causes big data performance issues

username The following query is working fine in my database but creating huge performance issues in the customer database. I know that I am using IN clause in where condition which is giving me this problem. But I don't know how to fix it. declare @AccountId i

grammatical issues where clauses are located

username Trying to figure out why it doesn't compile? I just posted a similar question about haskell's "where" syntax. primeFactors :: Int -> [Int] primeFactors x = genPrimes x [] where genPrimes x xs |x == 0 = [] |isPrime x = x : genPrimes (

grammatical issues where clauses are located

username Trying to figure out why it doesn't compile? I just posted a similar question about haskell's "where" syntax. primeFactors :: Int -> [Int] primeFactors x = genPrimes x [] where genPrimes x xs |x == 0 = [] |isPrime x = x : genPrimes (

Performance issues when using the WHERE EXISTS condition

Vladimir Hercules I have two tables with GB of data. When I execute a query with "WHERE EXISTS..." on either table, the whole MySQL fails. Query example: DELETE FROM `records` where exists ( select * from `measurements` where `file_id` = 17

Why using OR condition instead of Union causes performance issues

desk lamp Hi I have the following query in SP @CrmContactId is a parameter of SP. Select distinct A.PolicyBusinessId, A.PolicyDetailId from TPolicyBusiness A inner join TPolicyOwner B on a.PolicyDetailId=b.PolicyDetailId Left Join TAdditionalOwner C on c.Poli

Why using OR condition instead of Union causes performance issues

desk lamp Hi I have the following query in SP @CrmContactId is a parameter of SP. Select distinct A.PolicyBusinessId, A.PolicyDetailId from TPolicyBusiness A inner join TPolicyOwner B on a.PolicyDetailId=b.PolicyDetailId Left Join TAdditionalOwner C on c.Poli

Using multiple clauses in where

John What is the correct way to include multiple wheres in a LINQ call OR List<Pos> posList = DbContext.PosList .Where<Pos>(p => p.Pos == "51000785" || p => p.Pos == "123")

SQL delete* causes performance issues

curious programmer I have a PL/SQL file that at one point needs to drop an entire table. The challenges are: truncate table cannot be used because the relevant DB user cannot be given permission to execute DDL commands (due to SOX compliance) delete * works we

Avoid performance issues with runtime polymorphism

Ethan Coon In 10 hours of numerical code running on thousands of processors, I have a base class (Mesh) whose methods are hit 100 to 1000 trillion times. There are currently two (Mesh_A, Mesh_B) derived classes, but eventually it will expand to three or four.

Avoid annotating @Where clauses in models (Hibernate)

Annoirq I have in my model: @Entity @Where(clause="is_deleted <> '1'") public class Ad { In 95% of cases I will use undeleted ads. However, for one method, I need to get all the removed ads. How can I avoid Whereusing this clause in the query? I am using Crit

Avoid annotating @Where clauses in models (Hibernate)

Annoirq I have in my model: @Entity @Where(clause="is_deleted <> '1'") public class Ad { In 95% of cases I will use undeleted ads. However, for one method, I need to get all the removed ads. How can I avoid Whereusing this clause in the query? I am using Crit

How to improve eXSLT performance issues when using functions

trumpet TL; DR: It appears that running eXSLT is much slower than running eXSLT in XSLT2. (7 minutes vs 18 hours) Below, I've written two implementations of the same transformation in eXSLT and XSLT2 to explain my problem. Of course the engines are different,

How to improve eXSLT performance issues when using functions

trumpet TL; DR: It appears that running eXSLT is much slower than running eXSLT in XSLT2. (7 minutes vs 18 hours) Below, I've written two implementations of the same transformation in eXSLT and XSLT2 to explain my problem. Of course the engines are different,