I comandi HANDLER

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

Syntax:

HANDLER nome_tabella OPEN [ [AS] alias]
HANDLER nome_tabella READ nome_indice { = | >= | <= | < } (valore1,valore2,...)
    [ WHERE condizione ] [LIMIT ... ]
HANDLER nome_tabella READ nome_indice { FIRST | NEXT | PREV | LAST }
    [ WHERE condizione ] [LIMIT ... ]
HANDLER nome_tabella READ { FIRST | NEXT }
    [ WHERE condizione ] [LIMIT ... ]
HANDLER nome_tabella CLOSE

Spiegazione

L'istruzione HANDLER fornisce un accesso diretto alle interfacce di tabella dello Storage Engine per le ricerche con chiave e le scansioni delle tabelle. E' disponibile almeno per Aria, Memory (a partire da MariaDB 5.3), MyISAM e InnoDB (dovrebbe funzionare con la maggior parte degli Storage Engine 'normali', ma non con le tabelle di sistema, le MERGE e le viste).

A partire da MariaDB 5.3, è possibile utilizzare i Prepared Statement con HANDLER READ, e questo migliora notevolmente le performance (50%) perché non avviene un parsing e tutti i dati vengono trasformati in binari (non vengono convertiti in testo, come avviene con il protocollo normale).

Ricerche per chiave

Una ricerca per chiave si ottiene così:

HANDLER nome_tabella READ nome_indice { = | >= | <= | < }  (valore,valore) [LIMIT...]

I valori sono quelli delle varie colonne della chiave. Per la maggior parte dei tipi di indice (ad eccezione delle chiavi HASH nelle tabelle MEMORY) è possibile utilizzare un sottoinsieme dei prefissi delle colonne.

Se si fa uso di LIMIT, in caso di >= o > si verifica un NEXT implicito, mentre se si utilizza <= o < avviene un PREV implicito.

Dopo la lettura iniziale, è possibile utilizzare:

HANDLER nome_tabella READ nome_indice NEXT [ LIMIT ... ]
o
HANDLER nome_tabella READ nome_indice PREV [ LIMIT ... ]

per scansire la tabella nell'ordine definito dalla chiave.

Si noti che l'ordine delle righe non è definito per le chiavi che hanno valori duplicati e varia a seconda dello Storage Engine.

Scansioni per chiave

E' possibile scansionare una tabella nell'ordine definito dalla chiave in questo modo:

HANDLER nome_tabella READ nome_indice FIRST [ LIMIT ... ]
HANDLER nome_tabella READ nome_indice NEXT  [ LIMIT ... ]

Oppure, se l'handler supporta la scansione in ordine inverso (di solito è così):

HANDLER nome_tabella READ nome_indice LAST [ LIMIT ... ]
HANDLER nome_tabella READ nome_indice PREV [ LIMIT ... ]

Scansione delle tabelle

E' possibile effettuare la scansione completa della tabella seguendo l'ordine fisico delle righe in questo modo:

HANDLER nome_tabella READ FIRST [ LIMIT ... ]
HANDLER nome_tabella READ NEXT  [ LIMIT ... ]

Finding 'old rows'

HANDLER READ is not transactional safe, consistent or atomic. It's ok for the storage engine to returns rows that existed when you started the scan but that was later deleted. This can happens as the storage engine may cache rows as part of the scan from a previous read.

You may also find rows committed since the scan originally started.

Limitations

As this is a direct interface to the storage engine, some limitations may apply for what you can do and what happens if the table changes. Here follows some of the common limitations:

  • If you do an ALTER TABLE, all your HANDLER's for that table are automatically closed.
  • If you do an ALTER TABLE for a table that is used by some other connection with HANDLER, the ALTER TABLE will wait for the HANDLER to be closed.
  • For HASH keys, you must use all key parts when searching for a row.
  • For HASH keys, you can't do a key scan of all values. You can only find all rows with the same key value.
  • While each HANDLER READ command is atomic, if you do a scan in many steps, then some engines may give you error 1020 if the table changed between the commands. Please refer to the specific engine handler page if this happens.

Error codes

  • Error 1031 (ER_ILLEGAL_HA) Table storage engine for 't1' doesn't have this option
    • If you get this for HANDLER OPEN it means the storage engine doesn't support HANDLER calls.
    • If you get this for HANDLER READ it means you are trying to use a incomplete HASH key.
  • Error 1020 (ER_CHECKREAD) Record has changed since last read in table '...'
    • This means that the table changed between two reads and the handler can't handle this case for the given scan.

Check also the KB pages for the specific handler for the it's limitations when it comes to HANDLER calls.

See Also:

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.