1. Trang chủ
  2. » Công Nghệ Thông Tin

Phát triển web với PHP và MySQL - p 29 pps

10 212 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

11 ADVANCED MY SQL Advanced MySQL C HAPTER 11 255 + + | Grants for bookorama@% | + + |GRANT USAGE ON *.* TO 'bookorama'@'%' IDENTIFIED BY PASSWORD '6a87b6810cb073de' | |GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON books.* TO 'bookorama'@'%' | + + If you leave the database parameter off, the SHOW COLUMNS statement will default to the data- base currently in use. You can also use the table.column notation: show columns from books.orders; One other very useful variation of the SHOW statement 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; FIGURE 11.1 The output of the SHOW GRANTS statement. The GRANT statements 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 SHOW statement. A summary of all the variations is shown in Table 11.6. T ABLE 11.6 SHOW Statement Syntax Variation Description 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 database if specified, optionally with table names like table. 14 7842 CH11 3/6/01 3:35 PM Page 255 SHOW 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 FIELDS instead 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 LIKE clause 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 LIKE clause 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 FULL displays 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 database if it is spec- ified, optionally with a wildcard match. This information includes the table type and when each table was last updated. Using MySQL P ART II 256 TABLE 11.6 Continued Variation Description 14 7842 CH11 3/6/01 3:35 PM Page 256 SHOW GRANTS FOR user Shows the GRANT statements required to give the user specified in user his current level of privilege. Getting Information About Columns with DESCRIBE As an alternative to the SHOW COLUMNS statement, you can use the DESCRIBE statement, similar to the DESCRIBE statement 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 column is specified. You can use wildcards in the column name if you like. Understanding How Queries Work with EXPLAIN The EXPLAIN statement can be used in two ways. First, you can use EXPLAIN table; This gives very similar output to DESCRIBE table or SHOW COLUMNS FROM table. The second and more interesting way you can use EXPLAIN allows you to see exactly how MySQL evaluates a SELECT query. To use it this way, just put the word explain in front of a SELECT statement. You can use the EXPLAIN statement 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 EXPLAIN command 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%’; Advanced MySQL C HAPTER 11 11 ADVANCED MY SQL 257 TABLE 11.6 Continued Variation Description 14 7842 CH11 3/6/01 3:35 PM Page 257 Using MySQL P ART II 258 + + + + + + + + + | table | type | possible_keys | key | key_len | ref |rows | Extra | + + + + + + + + + | orders | ALL | PRIMARY | NULL | NULL | NULL | 4 | | | order_items | ref | PRIMARY | PRIMARY | 4 | orders.orderid | 1 | Using index | | customers | ALL | PRIMARY | NULL | NULL | NULL | 3 | where used | | books | eq_ref | PRIMARY | PRIMARY | 13 | order_items.isbn | 1 | where used | + + + + + + + + + FIGURE 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 type column 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. TABLE 11.7 Possible Join Types as Shown in Output from EXPLAIN Type Description const or system The table is read from only once. This happens when the table has exactly one row. The type system is used when it is a system table, and the type const otherwise. 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. 14 7842 CH11 3/6/01 3:35 PM Page 258 In 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 (orders and customers) are joined by using ALL; that is, by looking at every single row in the table. The rows column 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_keys column 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 PRIMARY keys. The key column is either the key from the table MySQL actually used, or NULL if no key was used. You’ll notice that, although there are possible PRIMARY keys for the orders and customers tables, they were not used in this query. We’ll look at how to fix this in a minute. The key_len column 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_items and books), the full key was used. The ref column shows the columns used with the key to select rows from the table. Finally, the Extra column 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 Value Meaning 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. WHERE used A WHERE clause is being used to select rows. Advanced MySQL C HAPTER 11 11 ADVANCED MY SQL 259 14 7842 CH11 3/6/01 3:35 PM Page 259 There 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 myisamchk utility. 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 Using MySQL P ART II 260 + + + + + + + + + | table | type | possible_keys | key | key_len | ref ________________| rows | Extra | + + + + + + + + + | books | ALL | PRIMARY | NULL | NULL | NULL | 4 | where used | | order_items | index | PRIMARY | PRIMARY | 17 | NULL | 5 | where used; Using index | | orders | eq_ref | PRIMARY | PRIMARY | 4 | order_items.orderid | 1 | | | customers | eq_ref | PRIMARY | PRIMARY | 4 | orders.customerid | 1 | | + + + + + + + + + FIGURE 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_ref for two of the tables and index for 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 FROM clause 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. 14 7842 CH11 3/6/01 3:35 PM Page 260 Speeding Up Queries with Indexes If you are in the situation mentioned previously, in which the possible_keys column from an EXPLAIN contains some NULL values, 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 using ALTER TABLE like 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. Advanced MySQL C HAPTER 11 11 ADVANCED MY SQL 261 14 7842 CH11 3/6/01 3:35 PM Page 261 You can also use the myisamchk utility 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 EXPLAIN as 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 INSERT statement. 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 HEAP tables extremely fast, but, in the event of a crash, your data will be lost. These char- acteristics make HEAP tables ideal for storing temporary or derived data. You should spec- ify the MAX_ROWS in the CREATE TABLE statement, or these tables can hog all your memory. Also, they cannot have BLOB, TEXT, or AUTO INCREMENT columns. Using MySQL P ART II 262 14 7842 CH11 3/6/01 3:35 PM Page 262 • BDB. These tables are transaction safe; that is, they provide COMMIT and ROLLBACK capabil- ities. They are slower to use than the MyISAM tables, 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.txt into 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 LOAD statement—see the MySQL manual for more details. To use the LOAD DATA INFILE statement, 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. Advanced MySQL C HAPTER 11 11 ADVANCED MY SQL 263 14 7842 CH11 3/6/01 3:35 PM Page 263 14 7842 CH11 3/6/01 3:35 PM Page 264 . 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,. 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. Advanced MySQL C HAPTER 11 11 ADVANCED MY SQL 263 14. CH11 3/6/01 3:35 PM Page 259 There 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

Ngày đăng: 06/07/2014, 19:20

Xem thêm: Phát triển web với PHP và MySQL - p 29 pps

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN