Share This
//Top 10 Best Practices in MySQL

Top 10 Best Practices in MySQL

MySQL is widely used around the world and is an open-source relational database management system. It has become popular due to its fast performance, high reliability, and ease of use. This article outlines some of the best practices in MySQL.

1. Always Use Appropriate Data Types

Use data types based on the nature of the data. Using inappropriate data types may take up more space or cause errors.

For example, using varchar(20) to store date-time values instead of the DATETIME datatype may lead to errors when updating time-related calculations and might result in invalid data storage.

2. Use CHAR(1) Instead of VARCHAR(1)

If the data is a single character string, use CHAR(1) instead of VARCHAR(1) because VARCHAR(1) will take additional bytes to store information.

3. Use CHAR Data Type for Fixed-Length Data

For example, using CHAR(1000) instead of VARCHAR(1000) will consume more space if the data length is less than 1000.

4. Avoid Using Regional Date Formats

When using DATETIME or DATE data types, always use the YYYY-MM-DD date format or the ISO standard date format compatible with your SQL engine. Other regional formats like DD-MM-YYYY, MM-DD-YYYY will not be stored correctly.

5. Set Indexes on Columns

Make sure that columns with indexes are used in JOIN statements to return query results quickly.

If you are using an UPDATE statement involving multiple tables, ensure that all columns used to join the tables are indexed.

6. Avoid Using Functions on Indexed Columns

Using functions on indexed columns does not take advantage of the index.

For example, if you want to fetch customer codes that start with “AK”, do not write:

SELECT columns FROM table WHERE left(customer_code, 2) = ‘AK’

Instead, rewrite it as follows:

SELECT columns FROM table WHERE customer_code LIKE ‘AK%’

This query will use the index for faster results.

7. Use SELECT * Only When Necessary

Do not write SELECT * in queries.

If there are many columns in the table, all of them will be returned, slowing down processing time.

Explicitly list only the columns you actually need.

8. Use ORDER BY Only When Necessary

Use ORDER BY when you want to display sorted results. Doing this in SQL can slow down the processing time in multi-user environments.

9. Choose the Right Database Engine

If you are developing an application that reads data more often than writing (e.g., a search engine), choose the MyISAM storage engine.

If you are developing an application that writes data more often than reading (e.g., real-time banking transactions), choose the InnoDB storage engine.

Choosing the wrong storage engine can affect performance.

10. Use EXISTS Wherever Necessary

If you want to check for data existence, do not use:

If (SELECT count(*) FROM Table WHERE col = ‘some value’) > 0

Instead, use EXISTS:

If EXISTS (SELECT * FROM Table WHERE col = ‘some value’)

This will process faster.

Source: https://bigdata-madesimple.com/top-10-best-practices-in-mysql/


APPLY NOW






    Benefits

    SALARY & BONUS POLICY

    RiverCrane Vietnam sympathizes staffs' innermost feelings and desires and set up termly salary review policy. Performance evaluation is conducted in June and December and salary change is conducted in January and July every year. Besides, outstanding staffs receive bonus for their achievements periodically (monthly, yearly).

    TRAINING IN JAPAN

    In order to broaden staffs' view about technologies over the world, RiverCrane Vietnam set up policy to send staffs to Japan for study. Moreover, the engineers can develop their career paths in technical or management fields.

    ANNUAL COMPANY TRIP

    Not only bringing chances to the staffs for their challenging, Rivercrane Vietnam also excites them with interesting annual trips. Exciting Gala Dinner with team building games will make the members of Rivercrane connected closer.

    COMPANY'S EVENTS

    Activities such as Team Building, Company Building, Family Building, Summer Holiday, Mid-Autum Festival, etc. will be the moments worthy of remembrance for each individual in the project or the pride when one introduces the company to his or her family, and shares the message "We are One".

    INSURANCE

    Rivercrane Vietnam ensures social insurance, medical insurance and unemployment insurance for staffs. The company commits to support staffs for any procedures regarding these insurances. In addition, other insurance policies are taken into consideration and under review.

    OTHER BENEFITS

    Support budget for activities related to education, entertainment and sports. Support fee for purchasing technical books. Support fee for getting engineering or language certificates. Support fee for joining courses regarding technical management. Other supports following company's policy, etc.

    © 2012 RiverCrane Vietnam. All rights reserved.

    Close