Thursday, November 13, 2008

13 useful graphing solutions for web developers

Many a web application can be enhanced with a decent graph to show data. Unfortunately out of the programming languages / frameworks that we have available to us, none seem to be very forthcoming with decent graph components, and it is left up to third party solutions to provide the necessary. From my hunts around the web the following showcases some of the better options. I’ve tried to keep these limited to the open source / free options.

LibChart

http://naku.dohcrew.com/libchart/pages/introduction/

Libchart is a simple PHP charting component, and it is totally free free free! The project is an ongoing one - with the last release in April 2008. If you are a PHP coder, you’ll love its simplicity.

Currently supports:

  • Bar charts (horizontal or vertical).
  • Line charts.
  • Pie charts.
  • Single or multiple data sets.
  • Compatibility with PHP 5.

PlotKit

http://media.liquidx.net/js/plotkit-doc/PlotKit.QuickStart.html

Primarily for Barcharts and Piecharts, plotkit uses some fancy Javascript, SVG and Canvas to do some wonderous things. Both these formats are gaining traction in the javascript community, and with implementations in major browser engines, we are deffo going to see more useful javascript drawing libraries cropping up over time.

FusionCharts Free

http://www.fusioncharts.com/free/

Fusioncharts offer some of the best animated flash charting components on the web, and luckily for us they also offer free components. Not only are they used commercial with clients such as Oracle, Microsoft and Dell amongst others, so you’d be in good company. FusionCharts Free can be distributed for free with your free or commercial softwares, irrespective of whether they’re open source or closed source. Hurray!

Chartpart

http://chartpart.com/

Google’s chart drawing API is pretty useful. More so when a little website generates and previews a chart automatically for you. Mighty useful.

Simile Widgets

http://code.google.com/p/simile-widgets/

The Simile project at MIT stands for Semantic Interoperability of Metadata and Information in unLike Environments, with alot of their work focused on visualising digital assets. Luckily for us, they have open sourced some useful widgets on Google code. The timeline is of particular interest, as it allows you to plot events as well as data, which can give some pretty cool results.

Simple Accessible Charts

http://grassegger.at/xperimente/charts-daten-semantik-css/

Inspired by some of the work done by ThinkVitamin - Out of all of the graphing solutions presented here, this is by far the simplest. HTML and CSS charts. Great if you are short on time, and need an easy bar chart.

AmCharts

http://www.amcharts.com/

Created by the talented Antanas Marcelionis Amcharts offer both a commercial charting solution and also offer a free linked version of their flash charting components. The project backups great components with even better documentation, with data (as you would expect) being pulled in via an XML file. They aso offer easy to use server side ASP.NET controls - which make the implementation even simpler for those of us who use the framework.

Features:

  • Stock
  • Column and Bar chart
  • Line and Area chart
  • Pie & Donut
  • Scatter and Bubble chart
  • Radar and Polar

JFreeChart

http://www.jfree.org/jfreechart/

If you develop on the java platform JFreechart is a 100% Java library that is used extensively in existing projects. Established eight years ago, the project has gone from strength to strength, and is now used by 40,000+ Java developers.

The library includes support for multiple chart types including gauges - common in many dashboard applications.

Charting Component Dojo Framework

http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/

We covered Dojo as one of the Javascript frameworks worth taking a serious look at. Dojo has a similar implementation of graphing via Canvas that we seen used in Plotkit. It is more extensive in its offering however, with cleaner lines generated overall.

Open Flash Chart

http://teethgrinder.co.uk/open-flash-chart-2/

Open Flash chart instead of XML as its datasource uses JSON, which in my opinion, opens it up for further expansion via javascript interaction. Now running on Flash Actionscript 3.0 model - open flash chart is one of the most impressive open source charting ciomponents on the Web today.

Zedgraph

http://zedgraph.org

Written in C# on the .NET framework, ZedGraph is a class library, user control, and web control for .net, for drawing 2D Line, Bar, and Pie Charts. You can easily implement the library in VB however with a binary download offered from SourceForge. Codeproject also offer a good example C# program for utilising the control.

Open source gauge component

http://www.digitaldarknet.net/flash/index.php?selector=dgauge

Gauge components are a new addition to many useful dashboards. These are open source flash swf files, and data passed in via javascript. Source files are not included, but these may be useful for small scale data display. There’s also a JS canvas sample available via the above link.

XML/SWF gauge

http://www.maani.us/gauge/

XML/SWF gauge is a simple, yet powerful tool to create attractive web gauges and dials from dynamic data.

Create an XML source to describe a gauge, then pass it to this tool’s flash file to generate the gauge. Type the XML source manually, or use any scripting language (PHP, Perl, ASP, CFML, etc.) to generate it automatically.

Wednesday, November 12, 2008

Khi nào và tại sao phải dùng Index trong Mysql

Một ngày kia bạn nhận ra rằng website của bạn chạy chậm đi, có thể là do đường truyền nhưng còn 1 nguyên nhân khác, đó là máy chủ server tính toán quá nhiều dẫn đến kết quả đưa ra chậm.

Khi số lượng record tăng lên thì thời gian tìm kiếm của bạn sẽ tăng đáng kể.
“Too many connections” không phải là hiếm gặp trong các website trên Internet.

Hãy xem xét ví dụ sau:

CREATE TABLE employee (
employee_number char(10) NOT NULL,
firstname varchar(40),
surname varchar(40),
address text,
tel_no varchar(25),
salary int(11),
overtime_rate int(10) NOT NULL
);

Và để tìm thông tin Lương của Nguyễn Nam (mã số 101802) , bạn sẽ query như sau:
SELECT salary FROM employee WHERE employee_number = ‘101832′;

MySQL biết rằng phải tìm ở table Employee nhưng nó sẽ không biết bắt đầu từ đâu. Thậm chí nó cũng không biết trước rằng có bao nhiêu kết quả . Do đó nó sẽ duyệt qua tất cả danh sách (vd Hơn 300000 người) để tìm thông tin về Nguyễn Nam.

Một trong những cách để giải quyết vấn đề là dùng Index cho table

Làm sao để quyết định Index cho những field nào hay làm index cho tất cả các field

===> Trả lời

- Index những field mà bạn thường xuyên dùng để làm tiêu chí search( ý nói ở đây là trong phần điều kiện where)
- Nên hạn chế index những field không cần thiết và thường xuyên bị update... vì DBMS cần thời gian để cập nhật cho file index.

Cách theo dõi "công việc tăng tốc" mà ta đang làm

Cú pháp: EXPLAIN Query;

Bằng lệnh này bạn sẽ nhận ra được với câu Query của bạn , điều gì đang xảy ra và kiểu kết hợp (Join) nào đang diễn ra bên trong.

mysql>> EXPLAIN SELECT employee_number,firstname,surname FROM employee WHERE employee_number= ‘10875′;
+———-+——-+—————+———+———+——-+——+——-+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+———-+——-+—————+———+———+——-+——+——-+
| employee | const | PRIMARY | PRIMARY | 10 | const | 1 | |
+———-+——-+—————+———+———+——-+——+——-+
Tốt hơn nhiều rồi , kiểu TYPE = Const có nghĩa rằng MYSQL hiểu ra chỉ có 1 hàng đúng với ý ta, và thể hiện qua cột Rows = 1 , kiểu key= PRIMARY được sử dụng và chiều dài key_len là 10.Chỉ tìm 1 hàng tất nhiên rằng tốt hơn nhiều so với tìm N hàng.

Vậy câu hỏi đặt ra là , nếu tôi muốn thêm Index cho những cột mà có thể có nhiều hơn 1 kết quả khi query thì sao? Vẫn add index như bình thường

Nhưng nên lưu ý vấn đề sau:
mysql>> ALTER TABLE employee ADD INDEX(surname,firstname);
mysql>> EXPLAIN SELECT overtime_rate FROM employee WHERE surname=’Nguyễn’ and firstname=”Nam”;
+——–+——+——————-+———+———+——-+——+———–+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+——–+——+——————-+———+———+——-+——+———–+
|employee| ref | surname,surname_2 | surname | 41 | const | 1 |where used |
+——–+——+——————-+———+———+——-+——+———–+

Tuy nhiên , nếu chỉ cần Firstname

mysql>> EXPLAIN SELECT overtime_rate FROM employee WHERE firstname=’Name’;
+———-+——+—————+——+———+——+——+————+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+———-+——+—————+——+———+——+——+————+
| employee | ALL | NULL | NULL | NULL | NULL | 9475| where used |
+———-+——+—————+——+———+——+——+————+
thì MySQL sẽ tìm hết vì không hề có Index cho Firstname mà chỉ có Index cho (Surname,Firstname)

Nếu chỉ muốn Index cho 1 phần??
Bạn không cần phải làm Index cho cả Field mà chỉ cần 1 phần. Giống như chi tiết Mục Lục của sách mà quá dài cũng làm bạn khá vất vả, do đó họ chỉ trích dẫn 1 tựa đề. Quay lại với table của chúng ta , Surname và Firstname chỉ maximum là 40 chars , nếu chúng ta index nó , chúng ta tạo ra mỗi record đến 80 chars . Có thể tiết kiệm bằng cách sau

mysql>> ALTER TABLE employee ADD INDEX(surname(20),firstname(20));


ĐIỀU KÌ DIỆU VỚI OPTIMIZE VÀ ANALYZE

“Ma thuật” của MySQL là biết cách chọn khoá (key) nào để query(nếu có). Quá trình này gọi là “query optimizer”, nó sẽ “liếc” qua những Index đang có để quyết định sẽ dùng Index nào để tìm. Hãy tưởng tượng bạn đang tìm 1 dĩa CD của “Maria Carrey” có tên là “I Love You” , có nghĩa là có 2 Indexes ở đây , 1 cho tên tác giả và 1 cho tên CD. Bạn nhận thấy rằng danh mục có 20000 tên tác giả và 400000 tên Album. Một cách đơn giản ,bạn sẽ tìm theo tên tác giả. Khi có được , bạn lại thấy rằng “Maria Carrey” có 50 CDs và CD “I Love You” bắt đầu bằng chữ I. Đơn giản và dễ dàng tìm ra cái mình muốn phải không ? MySQL cũng vậy nhưng …bạn phải chỉ cho nó bằng cách:

ANALYZE TABLE tablename;

Những lệnh DELETE và UPDATE để lại rất nhiều những khoảng trống (gaps) vô nghĩa cho table(Đặc biệt là khi bạn dùng kiểu varchar hay text/blob). Điều đó có nghĩa rằng MySQL cũng phải đọc và phân tích những thứ vô nghĩa đó khi query. Điều này được khắc phục khi bạn chạy

OPTIMIZE tablename;