Sql Query Optimizer Tool Mysql Commands Show
In this article we'll run through how to use EXPLAIN to write better MySQL. Query, the MySQL Query Optimizer tries. In your query and MySQL will show you.
Introduction MySQL and MariaDB are popular choices for database management systems. Both use the SQL querying language to input and query data. Although SQL queries are simple commands that are easy to learn, not all queries and database functions operate with the same efficiency. This becomes increasingly important as the amount of information you are storing grows and, if your database is backing a website, as your site's popularity increases. In this guide, we will discuss some simple measures you can take to speed up your MySQL and MariaDB queries. We will assume that you have already installed MySQL or MariaDB using one of our guides that is appropriate for your operating system.
Table Design Generalities One of the most fundamental ways to improve querying speed begins with the table structure design itself. This means that you need to begin considering the best way to organize your data before you begin using the software.
These are some questions that you should be asking yourself: How Will your Table Primarily be Used? Anticipating how you will use the table's data often dictates the best approach to designing a data structure. If you will be updating certain pieces of data often, it is often best to have those in their own table.
Failure to do this can cause the query cache, an internal cache maintained within the software, to be dumped and rebuilt over and over again because it recognizes that there is new information. If this happens in a separate table, the other columns can continue to take advantage of the cache. Updating operations are, in general, faster on smaller tables, while in-depth analysis of complex data is usually a task best relegated to large tables, as joins can be costly operations. What Kind of Data Types are Required? Sometimes, it can save you significant time in the long run if you can provide some restraints for your data sizes upfront. For instance, if there are a limited number of valid entries for a specific field that takes string values, you could use the 'enum' type instead of 'varchar'.
This data type is compact and thus quick to query. For instance, if you have only a few different kinds of users, you could make the column that handles that 'enum' with the possible values: admin, moderator, poweruser, user. Which Columns Will You be Querying? Knowing ahead of time which fields you will be querying repeatedly can dramatically improve your speed. Indexing columns that you expect to use for searching helps immensely.
You can add an index when creating a table using the following syntax: CREATE TABLE example_table ( id INTEGER NOT NULL AUTO_INCREMENT, name VARCHAR(50), address VARCHAR(150), username VARCHAR(16), PRIMARY KEY (id), INDEX (username) ); This would be useful if we knew that our users were going to be searching for information by username. This will create a table with these properties: explain example_table; +----------+--------------+------+-----+---------+----------------+ Field Type Null Key Default Extra +----------+--------------+------+-----+---------+----------------+ id int(11) NO PRI NULL auto_increment name varchar(50) YES NULL address varchar(150) YES NULL username varchar(16) YES MUL NULL +----------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) As you can see, we have two indices for our table. The first is the primary key, which in this case is the id field. The second is the index we've added for the username field. This will improve queries that utilize this field.
Although it is useful from a conceptual standpoint to think about which fields should be indexed during creation, it is simple to add indices to pre-existing tables as well. You can add one like this: CREATE INDEX index_name ON table_name( column_name); Another way of accomplishing the same thing is this: ALTER TABLE table_name ADD INDEX ( column_name ); Use Explain to Find Points to Index in Queries If your program is querying in a very predictable way, you should be analysing your queries to ensure that they are using indices whenever possible.
- вторник 11 декабря
- 69