3

I am running following delete statements i want to know if it is possible at all to combine them somehow:

DELETE from schools where visibility like 'revoked'

DELETE FROM bookmark_schools
WHERE school_id NOT IN (SELECT s.id FROM school s)

DELETE FROM school_addresses 
WHERE school_id NOT IN (SELECT s.id FROM school s)

DELETE FROM school_highlights 
WHERE school_id NOT IN (SELECT s.id FROM school s)

DELETE FROM school_images 
WHERE school_id NOT IN (SELECT s.id FROM school s)

...

And so on i have like 15 queries like this. Is it possible to run them together rather than running them individually?

NoviceMe
  • 3,126
  • 11
  • 57
  • 117

5 Answers5

0

ya it is possible,but one by one.but the question is whether you want to run it in the server or by some scripting language php.

Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32
0

You can join the query as below :

DELETE FROM bookmark_schools
FROM            bookmark_schools CROSS JOIN
                         school_addresses CROSS JOIN
                         school_highlights CROSS JOIN
                         school_images CROSS JOIN
                         schools CROSS JOIN
                         school
WHERE        (NOT (bookmark_schools.school_id IN
                             (SELECT        id
                               FROM            school AS s))) AND (NOT (school_addresses.school_id IN
                             (SELECT        id
                               FROM            school AS s))) AND (NOT (school_highlights.school_id IN
                             (SELECT        id
                               FROM            school AS s))) AND (NOT (school_images.school_id IN
                             (SELECT        id
                               FROM            school AS s))) AND (schools.visibility LIKE N'revoked')
Tharif
  • 13,794
  • 9
  • 55
  • 77
0

You can achieve what you want but with some conditions as per below-

step1: tables engine should be innodb as not work in myisam.

Step2: Make foreign key references means all child tables like bookmark_schools, school_Addresses etc. have a reference of master table schools with on delete cascade on.

Step3: Now you just need to delete rows from master table i.e. schools, and foreign key check will remove from all child tables.

Further you can take help from here.

Zafar Malik
  • 6,734
  • 2
  • 19
  • 30
0

To delete rows from multiple tables using a single query you will have to create an INNER JOIN on two tables. Refer this question

Community
  • 1
  • 1
Parag Jadhav
  • 1,853
  • 2
  • 24
  • 41
0

You can combine them into a single statement as others have indicated, but note that this will still be a single threaded operation.

If your goal is to improve performance / parallelize this operation, you may want to write a script which creates the 15 different client threads to run the statements concurrently.

Erix
  • 7,059
  • 2
  • 35
  • 61