Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Avoid common errors when working with MySQL

22 September 2020

Preamble

SQLS*Plus - 16497702708794A5706D1 AAF9 4E60 A8B3 1D976273A83E%203 optimize

​​

Working with the database is often the weakest point in the performance of many web applications. And you can often see how the same rake comes the developers in the design of the database. This set of tips will help you avoid many mistakes and learn a lot of useful things for yourself.

General

  • It is desirable to create a separate user for each individual database.
  • The database encoding may be any if it is UTF-8.
  • In most cases, it is better to use the InnoDB engine.
  • In PHP, it is better to forget about the highly outdated MySQL extension and use PDO or MySQLi if possible.
  • Do not open MySQL out without a special need.

It is better to make port forwarding:

ssh -fNL LOCAL_PORT:localhost:3306 REMOTE_USER@REMOTE_HOST

  • Use the hints from PROCEDURE ANALYSE(), which analyzes the structure of your table and provides tips for optimization. Note that this is only possible if you have real data in your table since the analysis is based on it.

Search and inquiries

  • If you need a single line when accessing the table, then use LIMIT 1. Using LIMIT 1 will be better because the database will stop the selection of records immediately after finding the row instead of choosing the whole table or index.

$r = mysql_query("SELECT * FROM user WHERE state = 'Alabama' LIMIT 1");
if (mysql_num_rows($r) > 0) { ... }

  • Besides the usual row index, you can index any column in the table. This will give you an increase in performance when searching.
  • Do not use ORDER BY RAND(). If you really need a random order of rows in your query, there are many different ways to do it differently. You will need to write additional code, but you will get rid of the performance bottleneck. ORDER BY RAND() slows down exponentially as the database grows.
  • Avoid using SELECT *. The more data is read from the table, the slower the query, which in turn increases the time spent working with the data warehouse. Also, if the database server is installed separately from the webserver, there will be a long delay in transferring data over the network.

It is better to do in the following way:

$r = mysql_query("SELECT username FROM user WHERE user_id = 1");
$d = mysql_fetch_assoc($r);
echo «Welcome {$d['username']}»;

Working with data

  • If the system is 32-bit, it makes no sense to set the UNSIGNED property for the INTEGER type, since such large numbers are not supported in PHP. But if you are using a 64-bit system then PHP supports large numbers, up to BIGINT with a sign.
  •  Linked “Foreign keys” tables should have similarities in the key structure.
  • It is best to use TINYINT(1) for storing Boolean values.
  • Money units are best stored in integers and only work with integers. For example, the number of kopecks to 100 means one ruble.

Working with lines

  • The best encoding for most databases is UTF-8.
  • Type TEXT can only hold 64 KB. In order to accommodate larger texts, you need to use LONGTEXT.
  • In versions of MySQL up to 5.0.3 VARCHAR was limited to 255 characters, but in older versions, the limit is 65535 characters.

Working with dates

  • When comparing DATETIME and TIMESTAMP, remember to do type conversion:

SELECT * FROM table WHERE `datetime` = DATE(`timestamp`)

  • Since TIMESTAMP is stored as UNIX_TIMESTAMP, it is possible to set the automatic update of the column.
  • DATE, TIME, DATETIME data types are output as rows, so different dates are searched and compared through conversion.

Working with transfers

  • To enumerate correctly use ENUM type. This is a very fast and compact field type where the values are stored as in TINYINT but displayed as in a string field.
  • As for any string, you can set enumerations to the default value.
  • The field with enumeration is stored as a number, so the speed is quite high.

Working with backups

  • It is better to use a backup with additional options -Q, -c, -e:

mysqldump -Q -c -e -u USER -pPASSWORD DATABASE > /path/to/file/dump.sql

  • -Q wraps names in reverse quotes.
  • -c makes a full insert, including the names of the columns.
  • -e makes the extended insert.
    The result is a smaller final file, and a slightly faster one is created.

Debug

  • If queries are slow, you can try to include a log for slow queries in /etc/mysql/my.cnf and then optimize queries via EXPLAIN.
  • It is convenient to use the program Mytop to monitor both queries and the server in general.

How to Avoid Common but Deadly MySQL Development Mistakes

Enteros

About Enteros

Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of RDBMS, NoSQL, and machine learning database platforms.

 
Tags:

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...