Posts Tagged ‘MySQL’

Mysql-Creating a Derived Data Column

Thursday, July 24th, 2008

Frequently we query our database for a value but then have to always do the same manipulation on it before we can use it. Well it can often be useful to do this “once” in the database and then store the manipulated value in a separate column of the database.

Here is a very simple example
INSERT INTO tbl_name (col1,col2) VALUES(15,col1*3.14159);

Where col2 is set to the value of col1*pi

You could also do more complex calculation/filtering/shortening in your code eg php,ASP etc

Mysql-Querying By Date

Thursday, July 24th, 2008

We are so frequently required to create date based queries, eg how many orders last week, last month, how many orders between two dates. It is necessary to avoid the trap of treating date as a number, instead convert the date into a handy unit such as days

# select all records from today
select * from tbl_orders where TO_DAYS(NOW()) = TO_DAYS(dtmdevdate);

# select all records from last three days
select * from tbl_orders where (TO_DAYS(NOW())-3) > TO_DAYS(orderdate);

MySQL provides a whole range of date functions become familiar with them here

Mysql-Test your MySQL queries inside a MySQL GUI before coding them

Thursday, July 24th, 2008

Before you start coding your application with PHP or whatever remember to test out your MySQL in your MySQL GUI such as SQLYOG or even the CLI

What are the advantages?

* You don’t have to worry about the Script/MySQL syntax clashes (quoting etc)
* You only have to think MySQL and not say PHP as well
* You can see the data produced or not produced
* You can test your concepts

Mysql-Synchronize data-types

Thursday, July 24th, 2008

Columns with identical information in different tables should be declared to have identical data types so that joins based on the corresponding columns will be faster.

Mysql-Use multiple-row inserts

Thursday, July 24th, 2008

Use multiple-row INSERT statements to store many rows with one SQL statement.

Mysql-Use statement priorities

Thursday, July 24th, 2008

* Use INSERT LOW_PRIORITY when you want to give SELECT statements higher priority than your inserts.
* Use SELECT HIGH_PRIORITY to get retrievals that jump the queue. That is, the SELECT is executed even if there is another client waiting.

Mysql-Insert delayed

Thursday, July 24th, 2008

Use insert delayed when you do not need to know when your data is written. This reduces the overall insertion impact because many rows can be written with a single disk write.

Mysql-Avoid variable-length column types when necessary

Thursday, July 24th, 2008

For MyISAM tables that change frequently, you should try to avoid all variable-length columns (VARCHAR, BLOB, and TEXT). The table uses dynamic row format if it includes even a single variable-length column.

Mysql-Run optimize table

Thursday, July 24th, 2008

his command defragments a table after you have deleted a lot of rows from it.

Mysql-Optimize where clauses

Thursday, July 24th, 2008

* Remove unnecessary parentheses
* COUNT(*) on a single table without a WHERE is retrieved directly from the table information for MyISAM  and MEMORY tables. This is also done for any NOT NULL expression when used with only one table.
* If you use the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table