SHOW TABLES [FROM database] Lists tables from the database [LIKE table] currently in use, or from the database called databaseif specified, optionally with table names like table... SHO
Trang 1+ - - - +
| G r a n t s f o r b o o k o r a m a @ % |
+ - - - +
| G R A N T U S A G E O N * * T O ' b o o k o r a m a ' @ ' % ' I D E N T I F I E D B Y P A S S W O R D ' 6 a 8 7 b 6 8 1 0 c b 0 7 3 d e ' |
+ - - - +
If you leave the database parameter off, the SHOW COLUMNSstatement will default to the
data-base currently in use You can also use the table.columnnotation:
show columns from books.orders;
One other very useful variation of the SHOWstatement can be used to see what privileges a user
has For example, if we run the following, we’ll get the output shown in Figure 11.1:
show grants for bookorama;
F IGURE 11.1
The output of the SHOW GRANTS statement.
The GRANTstatements shown are not necessarily the ones that were executed to give privileges
to a particular user, but rather summary equivalent statements that would produce the user’s
current level of privilege
The SHOW GRANTS statement was added in MySQL version 3.23.4—if you have an ear-lier version, this statement won’t work.
NOTE
There are many other variations of the SHOWstatement A summary of all the variations is
shown in Table 11.6
T ABLE 11.6 SHOW Statement Syntax
SHOW DATABASES Lists available databases, optionally
[LIKE database] with names like database
SHOW TABLES [FROM database] Lists tables from the database
[LIKE table] currently in use, or from the database called
databaseif specified, optionally with table names like table
Trang 2SHOW COLUMNS FROM table Lists all the columns in a particular table
[FROM database] [LIKE column] from the database currently in use, or from
the database specified, optionally with col-umn names like column You might use SHOW FIELDSinstead of SHOW COLUMNS
SHOW INDEX FROM table Shows details of all the indexes on a
[FROM database] particular table from the database currently
in use, or from the database called database
if specified You might use SHOW KEYS
instead
SHOW STATUS [LIKE status_item] Gives information about a number of system
items, such as the number of threads run-ning The LIKEclause is used to match against the names of these items, so, for example,‘Thread%’matches the items
‘Threads_cached’,‘Threads_connected’, and ‘Threads_running’
SHOW VARIABLES [LIKE variable_name] Displays the names and values of the
MySQL system variables, such as the ver-sion number The LIKEclause can be used
to match against these in a fashion similar
to SHOW STATUS
SHOW [FULL] PROCESSLIST Displays all the running processes in the
system, that is, the queries that are currently being executed Most users will see their own threads but if they have the PROCESS
privilege, they can see everybody’s processes—including passwords if these are
in queries The queries are truncated to 100 characters by default Using the optional keyword FULLdisplays the full queries
SHOW TABLE STATUS Displays information about each of the
[FROM database] [LIKE database] tables in the database currently being used,
or the database called databaseif it is spec-ified, optionally with a wildcard match This information includes the table type and when each table was last updated
T ABLE 11.6 Continued
Trang 3SHOW GRANTS FOR user Shows the GRANTstatements required to give
the user specified in userhis current level
of privilege
Getting Information About Columns with DESCRIBE
As an alternative to the SHOW COLUMNSstatement, you can use the DESCRIBEstatement, similar
to the DESCRIBEstatement in Oracle (another RDBMS) The basic syntax for it is
DESCRIBE table [column];
This will give information about all the columns in the table or a specific column if columnis
specified You can use wildcards in the column name if you like
Understanding How Queries Work with EXPLAIN
The EXPLAINstatement can be used in two ways First, you can use
EXPLAIN table;
This gives very similar output to DESCRIBE tableor SHOW COLUMNS FROM table
The second and more interesting way you can use EXPLAINallows you to see exactly how
MySQL evaluates a SELECTquery To use it this way, just put the word explainin front of a
SELECTstatement
You can use the EXPLAINstatement when you are trying to get a complex query to work and
clearly haven’t got it quite right, or when a query’s taking a lot longer to process than it
should If you are writing a complex query, you can check this in advance by running the
EXPLAINcommand before you actually run the query With the output from this statement, you
can rework your SQL to optimize it if necessary It’s also a handy learning tool
For example, try running the following query on the Book-O-Rama database It produces the
output shown in Figure 11.2
explain
select customers.name
from customers, orders, order_items, books
where customers.customerid = orders.customerid
and orders.orderid = order_items.orderid
and order_items.isbn = books.isbn
and books.title like ‘%Java%’;
11
T ABLE 11.6 Continued
Trang 4+ - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - +
| t a b l e | t y p e | p o s s i b l e _ k e y s | k e y | k e y _ l e n | r e f | r o w s | E x t r a |
+ - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - +
| o r d e r s | A L L | P R I M A R Y | N U L L | N U L L | N U L L | 4 | |
| c u s t o m e r s | A L L | P R I M A R Y | N U L L | N U L L | N U L L | 3 | w h e r e u s e d |
+ - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - +
F IGURE 11.2
The output of the EXPLAIN statement.
This might look confusing at first, but it can be very useful Let’s look at the columns in this table one by one
The first column,table, just lists the tables used to answer the query Each row in the result gives more information about how that particular table is used in this query In this case, you can see that the tables used are orders,order_items,customers, and books (We knew this already by looking at the query.)
The typecolumn explains how the table is being used in joins in the query The set of values this column can have is shown in Table 11.7 These values are listed in order from fastest to slowest in terms of query execution It gives you an idea of how many rows need to be read from each table in order to execute a query
T ABLE 11.7 Possible Join Types as Shown in Output from EXPLAIN
constor system The table is read from only once This happens when the table
has exactly one row The type systemis used when it is a system table, and the type constotherwise
eq_ref For every set of rows from the other tables in the join, we read
one row from this table This is used when the join uses all the parts of the index on the table, and the index is UNIQUE or is the primary key
ref For every set of rows from the other tables in the join, we read a
set of rows from this table which all match This is used when the join cannot choose a single row based on the join condition, that is, when only part of the key is used in the join, or if it is not UNIQUE or a primary key
range For every set of rows from the other tables in the join, we read a
set of rows from this table that fall into a particular range
index The entire index is scanned
ALL Every row in the table is scanned
Trang 5In the previous example, you can see that one of the tables is joined using eq_ref(books), and
one is joined using ref(order_items), but the other two (ordersand customers) are joined
by using ALL; that is, by looking at every single row in the table
The rowscolumn backs this up—it lists (roughly) the number of rows of each table that has to
be scanned to perform the join You can multiply these together to get the total number of rows
examined when a query is performed We multiply these numbers because a join is like a
prod-uct of rows in different tables—check out Chapter 9, “Working with Your MySQL Database,”
for details Remember that this is the number of rows examined, not the number of rows
returned, and that it is only an estimate—MySQL can’t know the exact number without
per-forming the query
Obviously, the smaller we can make this number, the better At present we have a pretty
negli-gible amount of data in the database, but when the database starts to increase in size, this query
would blow out in execution time We’ll return to this in a minute
The possible_keyscolumn lists, as you might expect, the keys that MySQL might use to join
the table In this case, you can see that the possible keys are all PRIMARYkeys
The keycolumn is either the key from the table MySQL actually used, or NULLif no key was
used You’ll notice that, although there are possible PRIMARYkeys for the ordersand
customerstables, they were not used in this query We’ll look at how to fix this in a minute
The key_lencolumn indicates the length of the key used You can use this to tell whether only
part of a key was used This is relevant when you have keys that consist of more than one
col-umn In this case, where the keys were used (order_itemsand books), the full key was used
The refcolumn shows the columns used with the key to select rows from the table
Finally, the Extracolumn tells you any other information about how the join was performed
The possible values you might see in this column are shown in Table 11.8
T ABLE 11.8 Possible Values for Extra Column as Shown in Output from EXPLAIN
Not exists The query has been optimized to use LEFT JOIN
Range checked for For each row in the set of rows from the other tables in the join,
each record try to find the best index to use, if any
Using filesort Two passes will be required to sort the data (This obviously takes
twice as long.) Using index All information from the table comes from the index—that is, the
rows are not actually looked up
Using temporary A temporary table will need to be created to execute this query
WHEREused A WHEREclause is being used to select rows
11
Trang 6There are several ways you can fix problems you spot in the output from EXPLAIN First, check column types and make sure they are the same This applies particularly to column width Indexes can’t be used to match columns if they have different widths You can fix this
by changing the types of columns to match, or building this in to your design to begin with Second, you can tell the join optimizer to examine key distributions and therefore optimize joins more efficiently using the myisamchkutility You can invoke this by typing
>myisamchk analyze pathtomysqldatabase/table
You can check multiple tables by listing them all on the command line, or by using
>myisamchk analyze pathtomysqldatabase/*.MYI
You can check all tables in all databases by running the following, which will produce the out-put shown in Figure 11.3:
>myisamchk analyze pathtomysqldatadirectory/*/*.MYI
+ - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - +
+ - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - +
| o r d e r _ i t e m s | i n d e x | P R I M A R Y | P R I M A R Y | 1 7 | N U L L | 5 | w h e r e u s e d ; U s i n g i n d e x |
| c u s t o m e r s | e q _ r e f | P R I M A R Y | P R I M A R Y | 4 | o r d e r s c u s t o m e r i d | 1 | |
F IGURE 11.3
This is the output of the EXPLAIN after running myisamchk.
You’ll notice that the way the query is evaluated has changed quite a lot We’re now only
using all the rows in one of the tables (books), which is fine In particular, we’re now using
eq_reffor two of the tables and indexfor the other MySQL is also now using the whole key for order_items(17 characters as opposed to 4 previously)
You’ll also notice the number of rows being used has actually gone up This is probably caused
by the fact that we have little data in the actual database at this point Remember that the num-ber of rows listed is only an estimate—try performing the actual query and checking this If these numbers are way off, the MySQL manual suggests using a straight join and listing the tables in your FROMclause in a different order
Third, you might want to consider adding a new index to the table If this query is a) slow, and b) common, you should seriously consider this If it’s a one-off query that you’ll never use again, such as an obscure report requested once, it won’t be worth the effort, as it will slow other things down We’ll look at how to do this in the next section
Trang 7Speeding Up Queries with Indexes
If you are in the situation mentioned previously, in which the possible_keyscolumn from an
EXPLAINcontains some NULLvalues, you might be able to improve the performance of your
query by adding an index to the table in question If the column you are using in your WHERE
clause is suitable for indexing, you can create a new index for it usingALTER TABLElike this:
ALTER TABLE table ADD INDEX (column);
General Optimization Tips
In addition to the previous query optimization tips, there are quite a few things you can do to
generally increase the performance of your MySQL database
Design Optimization
Basically you want everything in your database to be as small as possible You can achieve this
in part with a decent design that minimizes redundancy You can also achieve it by using the
smallest possible data type for columns You should also minimize NULLs wherever possible,
and make your primary key as short as possible
Avoid variable length columns if at all possible (like VARCHAR,TEXT, and BLOB) If your tables
have fixed-length fields they will be faster to use but might take up a little more space
Permissions
In addition to using the suggestions mentioned in the previous section on EXPLAIN, you can
improve the speed of queries by simplifying your permissions We discussed earlier the way
that queries are checked with the permission system before being executed The simpler this
process is, the faster your query will run
Table Optimization
If a table has been in use for a period of time, data can become fragmented as updates and
deletions are processed This will increase the time taken to find things in this table You can
fix this by using the statement
OPTIMIZE TABLE tablename;
or by typing
>myisamchk -r table
at the command prompt
11
Trang 8You can also use the myisamchkutility to sort a table index and the data according to that index, like this:
>myisamchk sort-index sort-records=1 pathtomysqldatadirectory/*/*.MYI
Using Indexes
Use indexes where required to speed up your queries Keep them simple, and don’t create indexes that are not being used by your queries You can check which indexes are being used
by running EXPLAINas shown previously
Use Default Values
Wherever possible, use default values for columns, and only insert data if it differs from the default This reduces the time taken to execute the INSERTstatement
Use Persistent Connections
This particular optimization tip applies particularly to Web databases We’ve already discussed
it elsewhere so this is just a reminder
Other Tips
There are many other minor tweaks you can make to improve performance in particular situa-tions and when you have particular needs The MySQL Web site offers a good set of additional tips You can find it at
http://www.mysql.com
Different Table Types
One last useful thing to discuss before we leave MySQL for the time being is the existence of different types of tables You can choose a table type when you create a table, using
CREATE TABLE table TYPE=type
The possible table types are
• MyISAM This is the default, and what we have used to date This is based on ISAM, which
stands for Indexed Sequential Access Method, a standard method for storing records and
files
• HEAP Tables of this type are stored in memory, and their indexes are hashed This makes
HEAPtables extremely fast, but, in the event of a crash, your data will be lost These char-acteristics make HEAPtables ideal for storing temporary or derived data You should spec-ify the MAX_ROWSin the CREATE TABLEstatement, or these tables can hog all your
memory Also, they cannot have BLOB,TEXT, or AUTO INCREMENTcolumns
Trang 9• BDB These tables are transaction safe; that is, they provide COMMITand ROLLBACK capabil-ities They are slower to use than the MyISAMtables, and are based on the Berkeley DB
At the time of writing, these were still being debugged in MySQL version 3.23.21, and will require an extra download in order to be used, available from the MySQL Web site
These additional table types can be useful when you are striving for extra speed or
transac-tional safety
Loading Data from a File
One useful feature of MySQL that we have not yet discussed is the LOAD DATA INFILE
state-ment This can be used to load table data in from a file It executes very quickly
This is a flexible command with many options, but typical usage is something like the
follow-ing:
LOAD DATA INFILE “newbooks.txt” INTO TABLE books;
This will read row data from the file newbooks.txtinto the table books By default, data fields
in the file must be separated by tabs and enclosed in single quotes, and each row must be
sepa-rated by a newline (\n) Special characters must be escaped out with a slash (\) All these
char-acteristics are configurable with the various options of the LOADstatement—see the MySQL
manual for more details
To use the LOAD DATA INFILEstatement, a user must have the FILE privilege discussed earlier
Further Reading
In these chapters on MySQL, we have focused on the uses and parts of the system most
rele-vant to Web development, and to linking MySQL with PHP
If you want to know more, particularly with regard to non-Web applications, or MySQL
administration, you can visit the MySQL Web site at
http://www.mysql.com
You might also want to consult Paul Dubois’ book MySQL, available from New Riders
Publishing
Next
We have now covered the fundamentals of PHP and MySQL In Chapter 12, “Running an
E-commerce Site,” we will look at the e-commerce and security aspects of setting up
database-backed Web sites
11