Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Indeks MySQL pada tampilan tidak berfungsi

Anda tidak dapat membuat indeks pada tampilan:http:/ /dev.mysql.com/doc/refman/5.7/en/view-restrictions.html , jadi Anda harus berharap indeks digunakan. https://stackoverflow.com/a/7922711/3595565

Solusi

Ada solusi yang disebutkan dalam komentar bagian lain dari dokumentasi:https://dev.mysql.com/doc/refman/5.5/en/create-view.html Di mana Anda membuat tabel biasa dan mengatur indeks khusus Anda dan memuat data dari tampilan ke dalam tabel.

LOCK TABLES materializedView WRITE; 
TRUNCATE materializedView; 
INSERT INTO materializedView SELECT * FROM regularView;
UNLOCK TABLES;

Mengapa Kueri Anda tidak menggunakan indeks?

Saat menggunakan UNION dalam SELECT mysql membuat tabel sementara untuk menyimpan data. Jadi, karena tampilan adalah 'jalan pintas' untuk kueri Anda yang lebih kompleks, saat memanggil pilih, tampilan akan menjalankan penyatuan lagi, gunakan tabel sementara... gunakan algoritme temptable untuk memproses data.

Memeriksa manual lagi:http://dev.mysql. com/doc/refman/5.7/en/view-restrictions.html

Kesimpulan :UNION dalam kueri Anda menghalangi tampilan untuk menggunakan indeks.

Sumber

pertanyaan di forum mysql untuk masalah yang sama jawaban:

laporan bug "JANGAN BUAT TABEL SEMENTARA UNTUK SEMUA UNION"

Diperbaiki di MySQL 5.7 http:/ /dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-3.html

Beberapa data uji untuk memeriksa profiler

CREATE TABLE test1 (
    id int auto_increment PRIMARY KEY,
  col1 varchar(50),
  col2 varchar(50)
);

CREATE TABLE test2 (
    id int auto_increment PRIMARY KEY,
  col1 varchar(50),
  col2 varchar(50)
);

INSERT INTO test1 (col1, col2) 
VALUES 
('test', 'testcol2'),
('test', 'testcol2'),
('test', 'testcol2'),
('test', 'testcol2'),
('test', 'testcol2'),
('test', 'testcol2');


INSERT INTO test2 (col1, col2) 
VALUES 
('test2', 'testcol2'),
('test2', 'testcol2'),
('test2', 'testcol2'),
('test2', 'testcol2'),
('test2', 'testcol2'),
('test2', 'testcol2');

CREATE VIEW testview AS
SELECT * FROM test1
UNION
SELECT * FROM test2;

Periksa profiler:

SET PROFILING = 1;
SELECT * FROM testview WHERE id = 1;
+----+-------+----------+
| id | col1  | col2     |
+----+-------+----------+
|  1 | test  | testcol2 |
|  1 | test2 | testcol2 |
+----+-------+----------+
SHOW PROFILE;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000017 |
| Waiting for query cache lock   | 0.000004 |
| checking query cache for query | 0.000029 |
| checking permissions           | 0.000006 |
| Opening tables                 | 0.000121 |
| System lock                    | 0.000012 |
| checking permissions           | 0.000014 |
| checking permissions           | 0.000032 |
| optimizing                     | 0.000004 |
| statistics                     | 0.000007 |
| preparing                      | 0.000006 |
| executing                      | 0.000003 |
| Sending data                   | 0.000046 |
| optimizing                     | 0.000003 |
| statistics                     | 0.000004 |
| preparing                      | 0.000003 |
| executing                      | 0.000002 |
| Sending data                   | 0.000023 |
| optimizing                     | 0.000003 |
| statistics                     | 0.000003 |
| preparing                      | 0.000003 |
| executing                      | 0.000002 |
| Sending data                   | 0.000008 |
| removing tmp table             | 0.000005 |
| Sending data                   | 0.000005 |
| Waiting for query cache lock   | 0.000002 |
| Sending data                   | 0.000024 |
| init                           | 0.000011 |
| optimizing                     | 0.000006 |
| statistics                     | 0.000004 |
| preparing                      | 0.000006 |
| executing                      | 0.000002 |
| Sending data                   | 0.000021 |
| end                            | 0.000003 |
| query end                      | 0.000004 |
| closing tables                 | 0.000002 |
| removing tmp table             | 0.000004 |
| closing tables                 | 0.000006 |
| freeing items                  | 0.000005 |
| Waiting for query cache lock   | 0.000003 |
| freeing items                  | 0.000013 |
| Waiting for query cache lock   | 0.000002 |
| freeing items                  | 0.000002 |
| storing result in query cache  | 0.000003 |
| logging slow query             | 0.000002 |
| cleaning up                    | 0.000003 |
+--------------------------------+----------+

Saya tidak dapat mengambil terlalu banyak informasi dari profil, tetapi itu menyebutkan tabel sementara, cukup (bagi saya) untuk memvalidasi kesimpulan saya.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Apakah proxy RDS memengaruhi penyatuan sisi aplikasi saat ini?

  2. Menyimpan HTML di MySQL:gumpalan atau teks?

  3. Menjalankan AMP (apache mysql php) di Android

  4. Bisakah saya mendefinisikan beberapa acara dalam satu deklarasi Pemicu di mysql?

  5. Mysql menonaktifkan kolom auto_increment sementara dalam prosedur Tersimpan