Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

May 022011
 

Order by predefined order in MySQL

Answer:

Assume you have a SQL statement like:

SELECT id FROM user WHERE id IN (5, 10, 6);

How to make sure the returned rows are in the exact order as 5, 10, 6?

You can do the following:

SELECT id FROM user WHERE id IN (5, 10, 6) ORDER BY FIELD (id, 5, 10, 6);

Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field

Apr 302011
 

Enable Expire header in Nginx

Answer:

Enabling Expire header for static contents is useful to reduce the loading of your web server.

If you are using nginx, it is easy by adding the following lines in your nginx.conf

location ~* \.(ico|css|js|gif|jpe?g|png)$ {
    expires max;
}

So all files with the above extensions will be delivered with the maximum expire header.

If you want user to update the cached contents, make sure you have change the path (i.e. rename) of these resources.

Apr 282011
 

Create Facebook Test Users

Answer:

In the last article, you already know how to get the Facebook access token. In this post, I will tell you how to create testing accounts for your app's testing.

# curl -F 'access_token=ACCESS_TOKEN' 'https://graph.facebook.com/APP_ID/accounts/test-users?installed=true&permissions=read_stream'

You can manipulate the permissions as needed.

Apr 272011
 

Explain query execution plan in MySQL

Answer:

To get a better understanding of the performance of your MySQL query, you can use the explain statement.

E.g.

mysql> explain select * from user where host = 'localhost';
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | user  | ref  | PRIMARY       | PRIMARY | 180     | const |   12 | Using where |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+

Usually you need to look at the column "key" to see if index was being used, and the column "rows" to see how many rows are scanned for the query.

Apr 242011
 

How to get my Facebook App' access token?

Answer:

Assume you have your created a Facebook application, and you have the two things: App ID & App Secret.

You can get the access token by a simple curl command.

# curl -F type=client_cred -F client_id=APP_ID -F client_secret=APP_SECRET https://graph.facebook.com/oauth/access_token