Posts

Showing posts from March, 2012

MySql Slow Queries

MySQL has a slow query log which can be enabled if you want to identify the queries that are causing problems for a website or application. Checking slow queries is one of the most important steps in optimizing and tuning mysql. This article shows how to identify those 'slow queries' that need special attention and proper optimization. First let’s check on the server if slow query logging is enabled on MySql. On Windows cd c:\xampp\mysql\bin bin>mysqladmin var | find "log_slow_queries" On linux bin>mysqladmin var |grep log_slow_queries Enable the slow query log MySQL prior to 5.1.0 requires a change to the MySQL my.cnf file and a restart in order to log slow queries. From MySQL 5.1.0 you can apparantly change this dynamically without having to restart. Too make the change permanent, you could add the following lines under the [mysqld] section of your my.ini or my.cnf configuration file: >vi /etc/my.cnf [mysqld] # Activate the Slow Q...

Kill a blocking (long running) sql statement

You can use the command line tool mysqladmin to view processes which returns the processes with their IDs. Then use the kill option of mysqladmin to kill your blocking process by its process ID. /* get process ID of blocking process */ mysql/bin>mysqladmin -uroot -p processlist /* kill it */ mysql/bin>mysqladmin -uroot -p kill PreceessID If you are using mysql client tool and already logged in as root user. mysql> show processlist; /* get process ID from output and replace with PreceessID */ mysql> kill PreceessID; Note: Do not kill commands like Alter table, optimize table, check table...

Where is my MySQL database on the server?

Usually /var/lib/mysql or /var/db/mysql directory used to store database and tales under UNIX like operating systems. You can use the following command to locate MySQL datadir: On windows: >mysqladmin variables | findstr "datadir" on Linux : >mysqladmin variables | grep datadir OR >grep datadir /etc/my.cnf

Basic Examples to Learn MySQL

Just make sure that you have installed MySQL database server with xampp and have a valid username & password to access. So developers get ready to learn MySQL within couple of hours. End of the tutorial you can do: Create and Drop Database Create, Alter, Truncate and Drop Table Select, Insert, Update and Delete rows Group by, Order by, Having, Union, Distinct Add, drop index, Explain command database backup with mysqldump or mysqlhotcopy read more here

Detecting user's screen size and resolution

If you're developing a site, you should carefully consider how your web pages appear on different kinds of screens. Today people access website on devices with different types of screens, you should account for some factors that affect the way your web pages appear on these devices. Javascript one simple way is to identify the screen width with the help of javascript and then render the webpage accordingly. Following is sample code to identify screen width and set it in cookie. <?php if(isset($_COOKIE["device_resolution"])) $screen_res = $_COOKIE["device_resolution"]; else //cookie is not found set it using Javascript { ?> <? } echo "Your screen resolution is set at ". $screen_res; ?> PHP With php, it is very easy to identify device using $_SERVER['HTTP_USER_AGENT']. Usining strpos() function find position of first occurrence of a string. <?php $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPho...

Export data to CSV from MySQL

Many times I get request from client for data dump. I use mysql database, and in mysql it is quite easy to create CSV files directly from MySQL with just one query! Let's say you want to export the email and name fields from your member table to a CSV file. Here is your code: SELECT email, name INTO OUTFILE '/tmp/data_dump_member.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM member Note : Make sure your MySQL server has write permissions to the location where you want to store the results file. You can either download the csv file from server's '/tmp' directory, or you can move this to http root directory and send link to client to download. cd /tmp/ mv data_dump_member.csv /var/www/html/ gzip -9 data_dump_member.csv