[Solved] Which will consume less resources? Sub query or two queries using programming language

EverSQL Database Performance Knowledge Base

Which will consume less resources? Sub query or two queries using programming language

here is Livi I am working on project in which database is too much huge and i have fear that db query optimization must be done there. I am working on DB and it has almost billions of records in tables and i am searching fast method which consume less resoureces from database server and web server. At the time, i am using 2 queries to select data from tables like

result1 = select data_id from table1 where id<1000

and than by using php code i fetch data and than again run query to fetch data like

select * from table2 where data_id1<result1

and i can also use this as sub query.

i have question that which method will be best for me as i told that it have very big database could be in GB's.

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 `table2` ADD INDEX `table2_idx_data_id1` (`data_id1`);
The optimized query:
SELECT
        * 
    FROM
        table2 
    WHERE
        table2.data_id1 < table2.result1

Related Articles



* original question posted on StackOverflow here.