Mysql
 sql >> Database >  >> RDS >> Mysql

Come posso chiedere aiuto per ottimizzare e correggere le query in MySQL?

Usa MOSTRA CREA TABELLA

Questo mi dice di più sulle tue tabelle di quanto le tue parole potrebbero mai:

mysql> show create table magic\G
*************************** 1. row ***************************
       Table: magic
Create Table: CREATE TABLE `magic` (
  `id` int(11) DEFAULT NULL,
  `what` varchar(255) DEFAULT NULL,
  `the` datetime DEFAULT NULL,
  `heck` text,
  `soup_is_good` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

AVVISO :se hai 70 colonne nella tabella, ometti quelle non necessarie . Cosa è necessario?

  • Campi uniti su
  • Campi SELEZIONATI
  • Campi DOVE sono attivi

Usa SPIEGAZIONE

Questo mi permette di vedere come ottimizzare al meglio la tua query attualmente funzionante, ma presumibilmente lenta:

mysql> explain select *     from magic\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: magic
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 1
        Extra: 
1 row in set (0.00 sec)

Usa \G

Dover scorrere a destra è generalmente un inconveniente.

Normale:

mysql> select * from magic;
+------------+-------------------------------+---------------------+-------------------+--------------+
| id         | what                          | the                 | heck              | soup_is_good |
+------------+-------------------------------+---------------------+-------------------+--------------+
| 1000000000 | A really long text string yay | 2009-07-29 22:28:17 | OOOH A TEXT FIELD |        100.5 | 
+------------+-------------------------------+---------------------+-------------------+--------------+
1 row in set (0.00 sec)

Meglio:

mysql> select * from magic\G
*************************** 1. row ***************************
          id: 1000000000
        what: A really long text string yay
         the: 2009-07-29 22:28:17
        heck: OOOH A TEXT FIELD
soup_is_good: 100.5
1 row in set (0.00 sec)

AVVISO: \G ovviamente trasforma una riga di dati in più righe. Questo diventa ugualmente ingombrante per diverse righe di dati. Fai ciò che sembra migliore.

Usa un pastebin esterno per blocchi di dati odiosamente grandi:

Facci sapere le tue aspettative

  • Lento? - Non sappiamo cosa sia per te la lentezza. Secondi, minuti, ore? Aiuta a sapere.
  • Più veloce - Non lo sappiamo neanche noi. Quali sono le tue aspettative in termini di velocità?
  • Frequenza - È una query che prevedi di eseguire solo una volta? Quotidiano? Centinaia o migliaia di volte al giorno? Questo ci aiuta a sapere quando è Abbastanza buono .