One of the best ways to speed up your web application is to enable query caching in your database, which caches commonly used SQL queries in memory for virtually instant access by the next page that makes the same request.
The reason this method is so powerful is that you don't have to make any changes to your web application, you just have to sacrifice a little bit of memory. This isn't going to fix all of your problems, but it definitely can't hurt.
Note: if your application updates tables frequently, then the query cache will be constantly purged and you won't get much or any benefit from this. This is ideal for an application that mostly does reads against the database, such as a Wordpress blog. This also won't work if you are running on shared hosting.
Enable Caching with Server Running
The first thing you'll want to do is make sure that your installation of MySQL actually has query caching support available. Most distributions do, but you should check anyway.
You'll want to run this command from your MySQL console, which will tell you if query caching is available.
mysql> show variables like 'have_query_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_query_cache | YES |
+------------------+-------+
Don't mistake this as meaning that query caching is actually enabled, because most hosting providers aren't going to enable this by default. Oddly enough, my Ubuntu Feisty installation already had it enabled…
Next we'll need to check and see if query caching is enabled. We'll need to check more than one variable, so we may as well do it all at once by checking for the variable query%
mysql> show variables like 'query%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 8388608 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+---------+
Here's the important items in the list and what they mean:
* query_cache_size - This is the size of the cache in bytes. Setting this value to 0 will effectively disable caching.
* query_cache_type - This value must be ON or 1 for query caching to be enabled by default.
* query_cache_limit - This is the maximum size query (in bytes) that will be cached.
If the query_cache_size value is set to 0 or you just want to change it, you'll need to run the following command, keeping in mind that the value is in bytes. For instance, if you wanted to allocate 8MB to the cache we'd use 1024 * 1024 * 8 = 8388608 as the value.
SET GLOBAL query_cache_size = 8388608;
Similarly, the other options can be set with the same syntax:
SET GLOBAL query_cache_limit = 1048576;
SET GLOBAL query_cache_type = 1;
Now how do we tell if it's actually working? You can use the SHOW STATUS command to pull all the variables that start with "Qc" to take a look at what is going on under the hood.
mysql> SHOW STATUS LIKE 'Qc%';
+-------------------------+--------+
| Variable_name | Value |
+-------------------------+--------+
| Qcache_free_blocks | 65 |
| Qcache_free_memory | 201440 |
| Qcache_hits | 18868 |
| Qcache_inserts | 2940 |
| Qcache_lowmem_prunes | 665 |
| Qcache_not_cached | 246 |
| Qcache_queries_in_cache | 492 |
| Qcache_total_blocks | 1430 |
+-------------------------+--------+
8 rows in set (0.00 sec)
You'll notice in the stats that I have plenty of free memory left. If your server shows a lot of lowmem prunes, you might need to consider increasing this value, but I wouldn't spend too much memory on query caching for a web server… you need to leave memory available for apache, php, ruby, or whatever you are using.
Enable in Config File
If you want these changes to survive a reboot or restart of the mysql server, you'll need to add them into your /etc/mysql/my.cnf configuration file for MySQL. Note that it might be in a different location on your installation.
Open up the file using a text editor in sudo or root mode, and then add these values if they don't already exist in the file. If they do exist, just uncomment them.
query_cache_size = 268435456
query_cache_type=1
query_cache_limit=1048576
Query caching can significantly improve the speed of your web application, especially if your application does mostly reads. Monitor the status using the methods above and see how it works over time.
link : http://www.howtogeek.com/howto/programming/speed-up-your-web-site-with-mysql-query-caching/
Search
Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts
Monday, January 5, 2009
Sunday, October 26, 2008
Finding Duplicates with sql
SELECT email,
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )
Saturday, October 11, 2008
PHP MySQL Thai UTF8 TIS620
ทำความเข้าใจกับเครื่องหมาย
??????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????
เวลาดึงฐานข้อมูลจาก MySQL หรือ phpMyAdmin
ปัญหานี้เป็นปัญหาจริงหรือ
- ไม่ใช่ปัญหาแต่ว่าผู้ใช้ปรับตัวเข้ากับเวอร์ชั่นใหม่ไม่เป็น
ถ้าไม่ใช่ปัญหาแล้วจะทำยังไงกับ MySQL 5.x , 4.1.x ที่ไม่ยอมเป็นไทย
- เวลา Connect Database ให้เพิ่มในส่วนของ mysql_db_query($dbname,"SET NAMES tis620");
เพิ่มเข้าไปทุกครั้งมีมีการ Connect
ทำการเพิ่ม SET NAMES tis620 ไปแล้วแต่ phpMyAdmin เวลา Dump ข้อมูลมันก็ยังเป็น ????? อยู่อีก
- เปิด my.ini ขึ้นมาโดยกดที่ Start -> run พิมพ์ my.ini แล้วกด Enter
ในส่วนของ [client] ให้เพิ่ม
default-character-set = tis620
ในส่วนของ [mysqld] หลัง database directory ให้เพิ่ม
default-character-set = tis620
character-set-server = tis620
collation-server = tis620_thai_ci
init_connect = 'SET collation_connection = tis620_thai_ci'
init_connect = 'SET NAMES tis620'
จากนั้น Restart MySQL
แล้วคนใช้ UTF-8 จะทำยังไงในเมื่อเรา SET ทุกอย่างเป็น tis620 หมด
- คนใช้ UTF-8 ต้องมา mysql_db_query($dbname,"SET NAMES UTF8"); เองซะแล้ว !!
** ในการ Input ข้อมูลต่างๆใน phpMyAdmin นั้นจะไม่มีผลใดๆ กับผู้ใช้ UTF8
เมื่อก่อนไม่เห็นมีแบบนี้เลยแล้วเมื่อก่อนให้เลือก character set คืออะไร
- เมื่อก่อนเป็น Character Set แบบปลอมๆ คือข้อมูลต่างๆที่เก็บลงฐานข้อมูลมันคือ latin1 นั่นเอง
แล้วทำไม Latin1 มันใช้ภาษาไทยได้หละ แน่นอนอยู่แล้วมันใช้ได้เนื่องจาก latin1 มัน Key map แบบเดียวกะ keyboard ไทย
และประเทศอื่นๆ ก็เป็น Key Map เดียวกัน แต่สมัยนี้พวกภาษาจะไม่ขึ้นอยู่กับ Key Map ที่เราเห็นอยู่แล้ว (UTF-8)
*** และที่สำคัญในเวอร์ชั่น 4.0, 3.x มีให้เลือก character set ตรงนั้นไม่ได้มีความหมายว่า เก็บข้อมูลเป็นภาษาไทย
ความหมายของมันคือ เรืยงลำดับภาษาไทย ต่างหาก คนไทยจึงเข้าใจผิดๆ มานาน -_-' กับ tis620 ใน MySQL
ไม่ก็ลองย้อนไปดูได้ครับแม้ว่าจะเลือก charset เป็น latin1 มันก็เก็บข้อภาษาไทยได้ เพียงแต่เรียงลำดับภาษาไทยไม่ได้เท่านั้น
แล้วทำไมสมัยนี้ MySQL ทำไมยุ่งยากกว่าเดิมเยอะ
- มันไม่ได้ยุ่งยากหรอกครับ มันอยู่ที่เราจะปรับตัวมากกว่า อย่ายึดติดกับหนังสือตามห้องสมุด เพราะที่ท่านอ่านมันเป็นความรู้เก่าๆ
ผมไม่ได้บอกว่าหนังสือที่เค้าขายกํนมันไม่ดีนะครับ เพียงแต่อยากจะบอกว่าที่เค้าขายกัน มันตกยุคไปนานโขแล้ว
เริ่มต้นทำความเข้าใจกับ MySQL ใหม่ เพราะที่มองเห็นว่ายุ่งยากกว่าเดิมจริงๆ ไม่ใช่ ควรเรียกว่าเป็นทางเลือกใหม่จะเหมาะสมกว่า
เพราะว่าระดับภาษาของ MySQL จะเจาะลึกลงไปเยอะมากคือ
- ต้องกำหนดภาษาเมื่อ Connect to Database
- ต้องกำหนดภาษาเมื่อ Create Database (การเรียงลำดับ)
- ต้องกำหนดภาษาเมื่อ Create Table (การเรียงลำดับ)
- ต้องกำหนดภาษาเมื่อ Create Field (การเรียงลำดับ)
รู้ปัญหามานานแล้วทำไมพึ่งมาบอก ??
- เพราะผมเลิกใช้ TIS-620 ไปแล้ว ตอนนี้ใช้แต่ UTF-8 ดีกว่าเยอะ !!
ความเสถียรของ MySQL 5.x เป็นยังไงบ้างเมื่อเทียบกับตัวเวอร์ชั่นเก่าๆ
- เสถียรกว่าเวอร์ชั่นเก่าๆ มาก ไม่มีอาการ Crash อิดๆ ออดๆ เหมือนแต่ก่อน ดังนั้น แนะนำให้ใช้เป็นอย่างยิ่ง
PHP5 ทำไมมี Bug เยอะจัง
- อันนี้ก็เข้าใจผิดๆ อีกเรื่องหนึ่งที่เจอกันเยอะมาก เข้าใหม่เสียว่า ที่ดูเป็น Bug ที่เห็นนั่นแหละ
คือเราเองเขียน Source Code ผิด เนื่องจาก PHP5 มี Sesnsitive มากๆ เกี่ยวกับการเขียน Code หลังจากที่
ผมเลือกใช้ PHP5 ผมจึงรู้ว่าสมองอันน้อยนิดที่คิดว่าผมเขียนโปรแกรมถูกนั้น ปรากฏว่าเจอ Error กระจาย
ต้องมาไล่แก้ code ใหม่อีกครั้ง ซึ่ง Error ที่แจ้งออกมา เราจะเห็นได้ว่าเราเขียนผิดเองจริงๆ และทำให้ผมมองย้อน
กลับไปที่ PHP4 จึงทำให้รู้ว่า PHP4 นี่แหละที่มี Bug เยอะกว่า PHP5เพราะว่าไม่ยอมแจ้งข้อผิดพลาดขึ้นมาเลย
ทั้งๆ ที่มีผิดอยู่เห็นๆ บทพิสูจน์นี้ทดสอบบน Domain ที่อยู่ใน Hosting ผมกว่า 60 กว่าโดเมนสามารถทำงาน
ได้กับ PHP5 ได้อย่างไม่มีปัญหาใดๆ เลยแม้แต่น้อย อีกทั้ง PHP5 ยังทำงานเร็วกว่า PHP4 เพราะเนื่องจากว่า PHP5
เลือกใช้ Zend Engine 2 นั่นเองจึงทำให้ความเร็วเพิ่มขึ้นมาประมาณ 20% และลด Load CPU ของ Server
ลงไปเยอะมาก
แล้วแบบนี้ก็ต้องหันมาใช้ PHP5 แทนที่ PHP4 ใช่ไหม ?
- อันนี้ขึ้นอยู่กับความชอบของแต่ละคน แต่ถ้าให้ผมแนะนำใช้เถอะครับ เพราะตอนนี้ PHP6 เค้าจะออกมากอีกแล้ว
แต่ว่าตอนนี้คุณยังไม่ได้เริ่มต้นที่จะทดลองใช้ PHP5 เลย ขืนใช้ PHP4 แล้ว Upgrade เป็น PHP6
ผมรับประกันได้เลยว่าคุณต้องปวดหัวแบบสุดๆ แน่นอน !! แล้วจะมาบอกอีกไม่ได้ว่า PHP5 และ PHP6 เค้ามี Bug !
ใช้ PHP4 แทนแล้วกัน ก็ขอยืนยันอีกครั้งว่า Source Code ของคุณจะเป็น Source Code รุ่นโบราณที่สุดในโลก
จาก http://www.appservnetwork.com
??????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????
เวลาดึงฐานข้อมูลจาก MySQL หรือ phpMyAdmin
ปัญหานี้เป็นปัญหาจริงหรือ
- ไม่ใช่ปัญหาแต่ว่าผู้ใช้ปรับตัวเข้ากับเวอร์ชั่นใหม่ไม่เป็น
ถ้าไม่ใช่ปัญหาแล้วจะทำยังไงกับ MySQL 5.x , 4.1.x ที่ไม่ยอมเป็นไทย
- เวลา Connect Database ให้เพิ่มในส่วนของ mysql_db_query($dbname,"SET NAMES tis620");
เพิ่มเข้าไปทุกครั้งมีมีการ Connect
ทำการเพิ่ม SET NAMES tis620 ไปแล้วแต่ phpMyAdmin เวลา Dump ข้อมูลมันก็ยังเป็น ????? อยู่อีก
- เปิด my.ini ขึ้นมาโดยกดที่ Start -> run พิมพ์ my.ini แล้วกด Enter
ในส่วนของ [client] ให้เพิ่ม
default-character-set = tis620
ในส่วนของ [mysqld] หลัง database directory ให้เพิ่ม
default-character-set = tis620
character-set-server = tis620
collation-server = tis620_thai_ci
init_connect = 'SET collation_connection = tis620_thai_ci'
init_connect = 'SET NAMES tis620'
จากนั้น Restart MySQL
แล้วคนใช้ UTF-8 จะทำยังไงในเมื่อเรา SET ทุกอย่างเป็น tis620 หมด
- คนใช้ UTF-8 ต้องมา mysql_db_query($dbname,"SET NAMES UTF8"); เองซะแล้ว !!
** ในการ Input ข้อมูลต่างๆใน phpMyAdmin นั้นจะไม่มีผลใดๆ กับผู้ใช้ UTF8
เมื่อก่อนไม่เห็นมีแบบนี้เลยแล้วเมื่อก่อนให้เลือก character set คืออะไร
- เมื่อก่อนเป็น Character Set แบบปลอมๆ คือข้อมูลต่างๆที่เก็บลงฐานข้อมูลมันคือ latin1 นั่นเอง
แล้วทำไม Latin1 มันใช้ภาษาไทยได้หละ แน่นอนอยู่แล้วมันใช้ได้เนื่องจาก latin1 มัน Key map แบบเดียวกะ keyboard ไทย
และประเทศอื่นๆ ก็เป็น Key Map เดียวกัน แต่สมัยนี้พวกภาษาจะไม่ขึ้นอยู่กับ Key Map ที่เราเห็นอยู่แล้ว (UTF-8)
*** และที่สำคัญในเวอร์ชั่น 4.0, 3.x มีให้เลือก character set ตรงนั้นไม่ได้มีความหมายว่า เก็บข้อมูลเป็นภาษาไทย
ความหมายของมันคือ เรืยงลำดับภาษาไทย ต่างหาก คนไทยจึงเข้าใจผิดๆ มานาน -_-' กับ tis620 ใน MySQL
ไม่ก็ลองย้อนไปดูได้ครับแม้ว่าจะเลือก charset เป็น latin1 มันก็เก็บข้อภาษาไทยได้ เพียงแต่เรียงลำดับภาษาไทยไม่ได้เท่านั้น
แล้วทำไมสมัยนี้ MySQL ทำไมยุ่งยากกว่าเดิมเยอะ
- มันไม่ได้ยุ่งยากหรอกครับ มันอยู่ที่เราจะปรับตัวมากกว่า อย่ายึดติดกับหนังสือตามห้องสมุด เพราะที่ท่านอ่านมันเป็นความรู้เก่าๆ
ผมไม่ได้บอกว่าหนังสือที่เค้าขายกํนมันไม่ดีนะครับ เพียงแต่อยากจะบอกว่าที่เค้าขายกัน มันตกยุคไปนานโขแล้ว
เริ่มต้นทำความเข้าใจกับ MySQL ใหม่ เพราะที่มองเห็นว่ายุ่งยากกว่าเดิมจริงๆ ไม่ใช่ ควรเรียกว่าเป็นทางเลือกใหม่จะเหมาะสมกว่า
เพราะว่าระดับภาษาของ MySQL จะเจาะลึกลงไปเยอะมากคือ
- ต้องกำหนดภาษาเมื่อ Connect to Database
- ต้องกำหนดภาษาเมื่อ Create Database (การเรียงลำดับ)
- ต้องกำหนดภาษาเมื่อ Create Table (การเรียงลำดับ)
- ต้องกำหนดภาษาเมื่อ Create Field (การเรียงลำดับ)
รู้ปัญหามานานแล้วทำไมพึ่งมาบอก ??
- เพราะผมเลิกใช้ TIS-620 ไปแล้ว ตอนนี้ใช้แต่ UTF-8 ดีกว่าเยอะ !!
ความเสถียรของ MySQL 5.x เป็นยังไงบ้างเมื่อเทียบกับตัวเวอร์ชั่นเก่าๆ
- เสถียรกว่าเวอร์ชั่นเก่าๆ มาก ไม่มีอาการ Crash อิดๆ ออดๆ เหมือนแต่ก่อน ดังนั้น แนะนำให้ใช้เป็นอย่างยิ่ง
PHP5 ทำไมมี Bug เยอะจัง
- อันนี้ก็เข้าใจผิดๆ อีกเรื่องหนึ่งที่เจอกันเยอะมาก เข้าใหม่เสียว่า ที่ดูเป็น Bug ที่เห็นนั่นแหละ
คือเราเองเขียน Source Code ผิด เนื่องจาก PHP5 มี Sesnsitive มากๆ เกี่ยวกับการเขียน Code หลังจากที่
ผมเลือกใช้ PHP5 ผมจึงรู้ว่าสมองอันน้อยนิดที่คิดว่าผมเขียนโปรแกรมถูกนั้น ปรากฏว่าเจอ Error กระจาย
ต้องมาไล่แก้ code ใหม่อีกครั้ง ซึ่ง Error ที่แจ้งออกมา เราจะเห็นได้ว่าเราเขียนผิดเองจริงๆ และทำให้ผมมองย้อน
กลับไปที่ PHP4 จึงทำให้รู้ว่า PHP4 นี่แหละที่มี Bug เยอะกว่า PHP5เพราะว่าไม่ยอมแจ้งข้อผิดพลาดขึ้นมาเลย
ทั้งๆ ที่มีผิดอยู่เห็นๆ บทพิสูจน์นี้ทดสอบบน Domain ที่อยู่ใน Hosting ผมกว่า 60 กว่าโดเมนสามารถทำงาน
ได้กับ PHP5 ได้อย่างไม่มีปัญหาใดๆ เลยแม้แต่น้อย อีกทั้ง PHP5 ยังทำงานเร็วกว่า PHP4 เพราะเนื่องจากว่า PHP5
เลือกใช้ Zend Engine 2 นั่นเองจึงทำให้ความเร็วเพิ่มขึ้นมาประมาณ 20% และลด Load CPU ของ Server
ลงไปเยอะมาก
แล้วแบบนี้ก็ต้องหันมาใช้ PHP5 แทนที่ PHP4 ใช่ไหม ?
- อันนี้ขึ้นอยู่กับความชอบของแต่ละคน แต่ถ้าให้ผมแนะนำใช้เถอะครับ เพราะตอนนี้ PHP6 เค้าจะออกมากอีกแล้ว
แต่ว่าตอนนี้คุณยังไม่ได้เริ่มต้นที่จะทดลองใช้ PHP5 เลย ขืนใช้ PHP4 แล้ว Upgrade เป็น PHP6
ผมรับประกันได้เลยว่าคุณต้องปวดหัวแบบสุดๆ แน่นอน !! แล้วจะมาบอกอีกไม่ได้ว่า PHP5 และ PHP6 เค้ามี Bug !
ใช้ PHP4 แทนแล้วกัน ก็ขอยืนยันอีกครั้งว่า Source Code ของคุณจะเป็น Source Code รุ่นโบราณที่สุดในโลก
จาก http://www.appservnetwork.com
Subscribe to:
Posts (Atom)