Search code examples
mysqlsqljoinindexingsqlfiddle

Demonstration of performance benefit of indexing a SQL table


I've always heard that "proper" indexing of one's SQL tables is key for performance. I've never seen a real-world example of this and would like to make one using SQLFiddle but not sure on the SQL syntax to do so.

Let's say I have 3 tables: 1) Users 2) Comments 3) Items. Let's also say that each item can be commented on by any user. So to get item=3's comments here's what the SQL SELECT would look like:

SELECT * from comments join users on comments.commenter_id=users.user_id 
WHERE comments.item_id=3

I've heard that generally speaking if the number of rows gets large, i.e., many thousands/millions, one should put indices on the WHERE and the JOINed column. So in this case, comments.item_id, comments.commenter_id, and users.user_id.

I'd like to make a SQLFiddle to compare having these tables indexed vs. not using many thousands, millions rows for each table. Might someone help with generating this SQLFiddle?


Solution

  • I'm the owner of SQL Fiddle. It definitely is not the place for generating huge databases for performance testing. There are too many other variables that you don't (but should, in real life) have control over, such as memory, hdd configuration, etc.... Also, as a shared environment, there are other people using it which could also impact your tests. That being said, you can still build a small db in sqlfiddle and then view the execution plans for queries with and without indexes. These will be consistent regardless of other environmental factors, and will be a good source for learning optimization.