[Solved] How to get part of the column value in sybase where this column situated in a query which is inside other query?

EverSQL Database Performance Knowledge Base

How to get part of the column value in sybase where this column situated in a query which is inside other query?

I have a column named Cons in a table named Conses as Order::Resource(PPP32#BB300320LQ00J#AAAR05504) I have second table which depends on first table. i wants to get all data from the seconds table and i did it manually as below and result is ok

select * from so_db..item   where  id =('PPP32' )
                            and    sub_id =('BB300320LQ00J')
                            and    tem_id =('AAAR05504');

but i want to replace inside the parentheses with an other query which must get one part of the column mentioned above as follow:

id = first part of column before '#' character

sun_id = middle part of column between '#' character

tem_id = last part of column

select * from so_db..item   where  id =( select Cons from er_db..Conses where UsedBy_ = 'mmmf8c713f490f8133c00e16ffdea136add')
                            and    sub_id =(select Cons from er_db..Conses where UsedBy_ = 'mmmf8c713f490f8133c00e16ffdea136add')
                            and    tem_id =(select Cons from er_db..Conses where UsedBy_ = 'mmmf8c713f490f8133c00e16ffdea136add');

Does anybody know how to do this?

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 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.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `item` ADD INDEX `item_idx_id_sub_id_tem_id` (`id`,`sub_id`,`tem_id`);
The optimized query:
SELECT
        * 
    FROM
        so_db..item 
    WHERE
        so_db..item.id = (
            'PPP32'
        ) 
        AND so_db..item.sub_id = (
            'BB300320LQ00J'
        ) 
        AND so_db..item.tem_id = (
            'AAAR05504'
        )

Related Articles



* original question posted on StackOverflow here.