1

複数のデータベースがあり、各データベースには共通のテーブルがいくつかあります。DELETE FROM *.table_name WHERE col_name LIKE 'value';次のようなことをしたいOPTIMIZE TABLE *.table_name;

これは単一の MYSQL クエリで可能ですか? または、これらのクエリを各データベースで個別に実行する必要がありますか?

4

1 に答える 1

4

テーブルを持つデータベースが 3 つある場合mytable、これで十分です。

DELETE A.*,B.*,C.*
FROM db1.mytable A,db2.mytable B,db3.mytable C
WHERE A.col_name LIKE 'value'
AND   B.col_name LIKE 'value'
AND   C.col_name LIKE 'value';
OPTIMIZE TABLE db1.mytable,db2.mytable,db3.mytable;

試してみる !!!

更新 2013-05-31 18:27 EDT

INFORMATION_SCHEMA データベースと動的 SQL に基づくソリューションがあります。

解決

USE db1
SET @GivenTable = 'mytb';
SET @GivenCol   = 'dat';
SET @GivenValue = 'Burger King';
SET @x = 0;
SELECT GROUP_CONCAT(dbtb) INTO @OptimizeTableList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @x = 0;
SELECT GROUP_CONCAT(dbalias,'.*') INTO @DeleteTableList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @x = 0;
SELECT GROUP_CONCAT(dbtb,' AS ',dbalias) INTO @FromList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @x = 0;
SELECT GROUP_CONCAT(CONCAT(dbalias,'.',col,'=',
QUOTE(@GivenValue)) SEPARATOR ' AND ') INTO @WhereClause
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias,col
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @OptTableSQL = CONCAT('OPTIMIZE TABLE ',@OptimizeTableList);
SET @DeleteSQL = CONCAT('DELETE ',@DeleteTableList,' FROM ',@FromList,' WHERE ',@WhereClause);
PREPARE st FROM @DeleteSQL;   EXECUTE st; DEALLOCATE PREPARE st;
PREPARE st FROM @OptTableSQL; EXECUTE st; DEALLOCATE PREPARE st;
SELECT * FROM db1.mytb;
SELECT * FROM db2.mytb;
SELECT * FROM db3.mytb;

サンプルデータ

DROP DATABASE IF EXISTS db1;
DROP DATABASE IF EXISTS db2;
DROP DATABASE IF EXISTS db3;
CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE DATABASE db3;
CREATE TABLE db1.mytb
(id int not null auto_increment,
dat varchar(20),
primary key (id),key (dat));
INSERT INTO db1.mytb (dat) VALUES
('McDonald''s'),('Wendy''s'),
('Burger King'),('Jack in the Box');
CREATE TABLE db2.mytb LIKE db1.mytb;
CREATE TABLE db3.mytb LIKE db1.mytb;
INSERT INTO db2.mytb (dat) SELECT dat FROM db1.mytb;
INSERT INTO db3.mytb (dat) SELECT dat FROM db1.mytb;
SELECT * FROM db1.mytb;
SELECT * FROM db2.mytb;
SELECT * FROM db3.mytb;

サンプルデータがロードされました

mysql> DROP DATABASE IF EXISTS db1;
Query OK, 1 row affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS db2;
Query OK, 1 row affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS db3;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE db2;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE db3;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE db1.mytb
    -> (id int not null auto_increment,
    -> dat varchar(20),
    -> primary key (id),key (dat));
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT INTO db1.mytb (dat) VALUES
    -> ('McDonald''s'),('Wendy''s'),
    -> ('Burger King'),('Jack in the Box');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> CREATE TABLE db2.mytb LIKE db1.mytb;
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE db3.mytb LIKE db1.mytb;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO db2.mytb (dat) SELECT dat FROM db1.mytb;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> INSERT INTO db3.mytb (dat) SELECT dat FROM db1.mytb;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM db1.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | McDonald's      |
|  2 | Wendy's         |
|  3 | Burger King     |
|  4 | Jack in the Box |
+----+-----------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM db2.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | Burger King     |
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM db3.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | Burger King     |
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
4 rows in set (0.00 sec)

mysql>

実行されたソリューション

mysql> USE db1
Database changed
mysql> SET @GivenTable = 'mytb';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @GivenCol   = 'dat';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @GivenValue = 'Burger King';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(dbtb) INTO @OptimizeTableList
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(dbalias,'.*') INTO @DeleteTableList
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(dbtb,' AS ',dbalias) INTO @FromList
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(CONCAT(dbalias,'.',col,'=',
    -> QUOTE(@GivenValue)) SEPARATOR ' AND ') INTO @WhereClause
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias,col
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.03 sec)

mysql> SET @OptTableSQL = CONCAT('OPTIMIZE TABLE ',@OptimizeTableList);
Query OK, 0 rows affected (0.00 sec)

mysql> SET @DeleteSQL = CONCAT('DELETE ',@DeleteTableList,' FROM ',@FromList,' WHERE ',@WhereClause);
Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE st FROM @DeleteSQL;   EXECUTE st; DEALLOCATE PREPARE st;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

Query OK, 3 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE st FROM @OptTableSQL; EXECUTE st; DEALLOCATE PREPARE st;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

+----------+----------+----------+----------+
| Table    | Op       | Msg_type | Msg_text |
+----------+----------+----------+----------+
| db1.mytb | optimize | status   | OK       |
| db2.mytb | optimize | status   | OK       |
| db3.mytb | optimize | status   | OK       |
+----------+----------+----------+----------+
3 rows in set (0.05 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM db1.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | McDonald's      |
|  2 | Wendy's         |
|  4 | Jack in the Box |
+----+-----------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM db2.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
3 rows in set (0.01 sec)

mysql> SELECT * FROM db3.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
3 rows in set (0.01 sec)

mysql>

クエリはどのように見えますか

mysql> SELECT @OptimizeTableList,@DeleteTableList,@FromList,
    -> @WhereClause,@OptTableSQL,@DeleteSQL\G
*************************** 1. row ***************************
@OptimizeTableList: db1.mytb,db2.mytb,db3.mytb
  @DeleteTableList: dbalias1.*,dbalias2.*,dbalias3.*
         @FromList: db1.mytb AS dbalias1,db2.mytb AS dbalias2,db3.mytb AS dbalias3
      @WhereClause: dbalias1.dat='Burger King' AND dbalias2.dat='Burger King' AND dbalias3.dat='Burger King'
      @OptTableSQL: OPTIMIZE TABLE db1.mytb,db2.mytb,db3.mytb
        @DeleteSQL: DELETE dbalias1.*,dbalias2.*,dbalias3.* FROM db1.mytb AS dbalias1,db2.mytb AS dbalias2,db3.mytb AS dbalias3 WHERE dbalias1.dat='Burger King' AND dbalias2.dat='Burger King' AND dbalias3.dat='Burger King'
1 row in set (0.00 sec)

mysql>

警告

それが機能するには、3 つのデータベースのいずれかに立っている必要があります。

あなたはそれがうまくいったことに驚いていますか??? ねえ、私もそうです!!!

multiple-tableDELETEの詳細については、MySQL ドキュメントを参照してください。というフレーズを検索しますmultiple-table DELETE statement

于 2013-05-31T21:08:51.560 に答える