Problem Solution Discussion Determining Which Table Types the Server Supports

This inform at ion can be useful for writ ing adm inist rat ive applicat ions. For exam ple, you m ight writ e a long- running program t hat probes t he server periodically t o m onit or it s act ivit y. A sim ple applicat ion of t his t ype m ight ask t he server t o report t he num ber of connect ions it s received and it s upt im e, t o det erm ine a running display of average connect ion act ivit y. The queries t o obt ain t his inform at ion are: SHOW STATUS LIKE Connections; SHOW STATUS LIKE Uptime; I f you want t o avoid having t o reconnect each t im e you issue t he queries, you can ask t he server for it s client t im eout period and probe it at int ervals short er t han t hat value. You can get t he t im eout value in seconds wit h t his query: SHOW VARIABLES LIKE wait_timeout; The default value is 28800 8 hours , but it m ay be different on your syst em . The MySQL Uncertainty Principle Heisenbergs uncert aint y principle for m easurem ent of quant um phenom ena has a MySQL analog. I f you m onit or MySQLs st at us t o see how it changes over t im e, you m ay not ice a curious effect for som e of t he indicat ors: Each t im e you t ake a m easurem ent , you change t he value youre m easuring For exam ple, you can det erm ine t he num ber of queries t he server has received by using t he following st at em ent : SHOW STATUS LIKE Questions However, t hat st at em ent is it self a query, so each t im e you issue it , you cause t he Questions value t o change. I n effect , your perform ance assessm ent inst rum ent cont am inat es it s own m easurem ent s, som et hing you m ay want t o t ake int o account .

9.18 Determining Which Table Types the Server Supports

9.18.1 Problem

You want t o know whet her you can creat e a t able using a given t able t ype.

9.18.2 Solution

Ask t he server which t able t ypes it support s.

9.18.3 Discussion

SHOW VARIABLES can t ell you whet her or not cert ain t able t ypes are available. For exam ple, you can t est t he have_bdb , have_innodb , and have_gemini variables t o see if t he server support s t ransact ion- safe t ables. The following PHP code dem onst rat es how t o check for a value of YES for t he have_bdb variable, using a t w o-st age approach. First , m ake sure t hat t he variable exist s by t est ing for a nonem pt y result set . The variable will not be present at all if your version of MySQL predat es t he inclusion of support for t he t able t ype. Then, fet ch t he variables value t o see if it s YES : avail = FALSE; escape the variable name properly var_name = ereg_replace [_], \\\\1, have_bdb; if result_id = mysql_query SHOW VARIABLES LIKE var_name, conn_id { if row = mysql_fetch_row result_id avail = row[1] == YES ? TRUE : FALSE; mysql_free_result result_id; } Aft er t his code execut es, avail w ill be TRUE or FALSE t o indicat e whet her t he server support s BDB t ables. To check for a different t able t ype, m odify t he code t o t est t he appropriat e server variable nam e. A m ore general approach is t o writ e a funct ion t hat checks for all known handlers and ret urns a list of t he ones t hat are support ed. You cannot ask t he server for a list of t ypes direct ly, but if you know what t he possible t ypes are, you can det erm ine which of t hem are support ed using t he following rules: • Prior t o MySQL 3.23, only t he I SAM t ype is available. • As of MySQL 3.23, MyI SAM is always available. Ot her t able handlers m ay also be available. For t he I SAM, BDB, and Gem ini t ypes, check t he have_isam , have_bdb , and have_gemini server variables look for a value of YES . I nnoDB t able availabilit y is indicat ed by have_innodb . However, t hat variable was at one t im e called have_innobase ; for com plet eness, check t hem bot h. These rules are im plem ent ed by t he Perl funct ion get_table_handlers show n below . I t ret urns a list cont aining t he words drawn from t he list bdb , gemini , innodb , isam , and myisam , corresponding t o t hose t able t ypes t hat are found t o be support ed: sub get_table_handlers { my dbh = shift; my types; my typemap = have_bdb = bdb, have_gemini = gemini, have_innodb = innodb, have_innobase = innodb, obsolete form of have_innodb have_isam = isam ; get server version number my ver_num = get_server_version dbh[1]; numeric form if ver_num 32300 only ISAM available prior to 3.23.xx { types = isam; } else { types = myisam; MyISAM always available as of 3.23.xx Issue SHOW VARIABLES query to get the have_ server variables that indicate presence of table handlers. There are some have_ variables that dont pertain to table types, but its still more efficient to issue a single query than a query for each variable. my sth = dbh-prepare SHOW VARIABLES LIKE have\\_; sth-execute ; while my var, val = sth-fetchrow_array { push types, typemap{var} if exists typemap{var} val eq YES; } } return sort types;

Chapter 10. Importing and Exporting Data

I nt roduct ion I m port ing Dat a wit h LOAD DATA and m ysqlim port Specifying t he Dat afile Locat ion Specifying t he Dat afile Form at Dealing wit h Quot es and Special Charact ers I m port ing CSV Files Reading Files from Different Operat ing Syst em s Handling Duplicat e I ndex Values Get t ing LOAD DATA t o Cough Up More I nform at ion Dont Assum e LOAD DATA Knows More t han I t Does Skipping Dat afile Lines Specifying I nput Colum n Order Skipping Dat afile Colum ns Export ing Query Result s from MySQL Export ing Tables as Raw Dat a Export ing Table Cont ent s or Definit ions in SQL Form at Copying Tables or Dat abases t o Anot her Server Wr it ing Your Own Export Program s Convert ing Dat afiles from One Form at t o Anot her Ext ract ing and Rearranging Dat afile Colum ns Validat ing and Transform ing Dat a Validat ion by Direct Com parison