Wednesday, September 28, 2016

MySQL Backup in PHP

found my old solution everything by hand not working (missing keys)

SHOW CREATE TABLE

SELECT for data

Saturday, November 28, 2015

Slow SELECT in MySQL

DESC query

  • Missing index
  • Unusable index: collation mismatch
  • Unusable index: %s% (only s% works)

SELECT statements in MySQL can sometimes be slow due to various reasons. Optimizing these queries is essential to ensure efficient database performance. Here are some common reasons for slow SELECT queries in MySQL and how to address them:

Lack of Indexes:

Without proper indexing, MySQL has to scan the entire table to find the required data, leading to slow SELECT queries. Ensure that you have appropriate indexes on the columns frequently used in WHERE clauses and JOIN conditions. For example:

CREATE INDEX idx_username ON users(username);

Inefficient Queries:

Complex or poorly written queries can result in slower execution. Use efficient SQL queries by avoiding unnecessary subqueries and selecting only the columns you need. Additionally, use LIMIT to restrict the number of rows returned if possible.

Large Data Sets:

SELECT queries on large tables can be slow. Consider pagination and LIMIT to fetch data in smaller chunks. This prevents the database from loading excessive data into memory.

Table Locking:

InnoDB, the default storage engine in MySQL, uses row-level locking to minimize contention. However, if your queries are not optimized or there's heavy write activity, it can lead to table locking and slower SELECT queries. Check your queries and transaction isolation levels.

Outdated MySQL Version:

Ensure that you are using an up-to-date version of MySQL. Newer versions often come with performance improvements and bug fixes.

Hardware and Server Resources:

Slow SELECT queries can also be a result of insufficient hardware resources. Make sure your server has enough CPU, memory, and I/O resources to handle the database workload efficiently.

Suboptimal Configuration:

MySQL's configuration parameters can greatly impact query performance. Tweaking settings like innodb_buffer_pool_size, innodb_log_file_size, and query_cache_size based on your system's requirements can lead to significant improvements.

Unoptimized Joins:

Complex JOIN operations can slow down SELECT queries. Ensure that you are using the appropriate types of JOINs (e.g., INNER JOIN, LEFT JOIN) and that the JOIN conditions are optimized.

Denormalized Tables:

While normalization is a good practice for database design, overly normalized tables can sometimes result in complex JOINs. Consider denormalizing certain parts of your schema if it improves query performance.

Missing Query Cache:

MySQL's query cache can help in speeding up SELECT queries, but it needs to be used judiciously. If you have frequently changing data, the query cache might not be as effective.

In conclusion, slow SELECT queries in MySQL can be attributed to a variety of factors. Proper indexing, efficient query writing, and careful consideration of the database schema can significantly improve performance. Regular monitoring and optimization are key to maintaining a high-performing MySQL database.

Unusable indexes can significantly impact the performance of your SELECT queries in MySQL. Here are some common issues related to unusable indexes and how to address them:

Using Wildcard '%' at the Beginning of a LIKE Pattern:

When you use a wildcard at the beginning of a LIKE pattern (e.g., LIKE '%str%'), MySQL can't use an index efficiently. Indexes are most effective when searching for patterns that start with a known value. To address this, you can use full-text indexing or change your query pattern to avoid the leading wildcard if possible.

Mismatched Data Types or Collations:

If the data types or collations of columns in your query don't match the indexed columns, MySQL won't use the indexes. Ensure that the data types and collations are consistent. If necessary, adjust your query or table schema to match.

Case Sensitivity Issues:

MySQL's default behavior is to be case-insensitive for indexes and comparisons. If you have a case-sensitive collation for a column but want a case-insensitive search, you can either change the column's collation or use the LOWER function to make the search case-insensitive. However, note that using LOWER may still limit index usage.

Here's an example of how to address these issues:

ALTER TABLE your_table MODIFY your_column VARCHAR(255) COLLATE utf8_general_ci;

SELECT * FROM your_table WHERE LOWER(your_column) LIKE '%str%';

Using Functions or Expressions in WHERE Clauses:

Using functions or expressions in your WHERE clauses can prevent index usage. Try to rewrite your query to use the column directly in the WHERE clause without any transformations.

Not Using Proper Index Hints:

In some cases, you may need to use index hints to instruct MySQL on which index to use. While this is not recommended for routine queries, it can be a last resort for specific scenarios. For example:

SELECT * FROM your_table USE INDEX (your_index) WHERE your_column LIKE 'str%';

Full-Text Search:

For more complex text searches, consider using MySQL's full-text search capabilities. Full-text indexes are designed to efficiently search for text within large amounts of data.

Optimizing unusable indexes in MySQL requires a combination of query design, schema design, and, in some cases, using specialized indexing techniques. Always monitor query performance and use MySQL's built-in tools like the EXPLAIN statement to understand how your queries are being executed and which indexes, if any, are used. This will help you fine-tune your database for better performance.

Monday, February 2, 2015

MySQL performance

Indexing of MySQL tables is crucial, too little is bad and too much is bad as well. Best practice is to work tightly with DESCRIBE command and index columns used in JOIN and WHERE clauses.

There are several situations, where indexes can’t be used. If you perform LIKE wildcard search, only col LIKE ’abc%’ or col LIKE ’a%c%’ use indexes, but col LIKE ’%abc%’ and col LIKE %abc’ and col LIKE col2 don’t – because of the way B-Tree index is designed. Or if you have to do a string operation before comparison, e.g. UPPER(col) LIKE ’ABC’, all rows has to be loaded. In such case consider adding a new column, with cached upper-case values.