Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting rows faster than filtering rows

Status
Not open for further replies.

aalnaif

Technical User
Jan 12, 2007
44
CA
I am creating a temporary table which is simply a copy of two tables joined together, excluding rows which meet a certain criteria. For some reason, it is much faster for me to make a full copy of the joined tables, and then delete from the table the rows which meet the criteria, than it is to filter the rows in the first place.

Code:
drop table if exists active_customers;
create temporary table active_customers
     (select *
     from customers as c, customer_status as s
     where c.customer_ID = s.customer_ID);

delete from active_customers
where activity_status = 'I';

is much faster than

Code:
drop table if exists active_customers;
create temporary table active_customers
     (select *
     from customers as c, customer_status as s
     where c.customer_ID = s.customer_ID
     and s.activity_status = 'I');

In case it matters, I should note that customer_ID is indexed in both the customers and the customer_status tables, and activity_status is an indexed column in the customer_status table. Does anyone know how it is possible for the first query to be faster than the second query.
 
Hi

No idea, maybe in the second case does not use the index... But anyway I would try to [tt]join[/tt] with [tt]using[/tt]. In other databases [tt]using[/tt] performs better than [tt]on[/tt]; hopefully this stands for MySQL too.
Code:
[b]select[/b] *
[b]from[/b] customers
[b]inner join[/b] customer_status [b]as[/b] s [b]using[/b] (customer_ID)
[b]where[/b] s.activity_status = [i]'I'[/i]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top