[Solved] SSIS: How to check if string value from one column of a table matches with string value (from the right) in a column of another table?

EverSQL Database Performance Knowledge Base

SSIS: How to check if string value from one column of a table matches with string value (from the right) in a column of another table?

I am pretty new to SSIS and below is the SQL query which explains what I am trying to do:

select * from Table1 t1 join 
(Select ID, LEN(ID) as len from Table2) as t2
on RIGHT(RTRIM(t1.col) , t2.len) = t2.ID

There might be some better query in SQL for the same but I am intrested as how to implement this logic in SSIS?

I need to find all the records from table 1 where the col (from the right) matches with the ID value retrieved from table 2.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Calling Functions With Indexed Columns (query line: 14): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `col` is indexed, the index won’t be used as it’s wrapped with the function `RIGHT`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 14): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `len` is indexed, the index won’t be used as it’s wrapped with the function `RIGHT`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  4. Avoid Subqueries (query line: 7): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
The optimized query:
SELECT
        * 
    FROM
        Table1 t1 
    JOIN
        (
            SELECT
                Table2.ID,
                LEN(Table2.ID) AS len 
            FROM
                Table2
        ) AS t2 
            ON 
    RIGHT(RTRIM(t1.col), t2.len) = t2.ID

Related Articles



* original question posted on StackOverflow here.