Skip to content

# Month: May 2009

## SQL: finding a user’s country/region based on IP

MySQL Indexing Performance SQL May 26, 2009December 16, 2009

I’ve encountered the same problem twice for different customers, so I guess it’s worth a discussion.

A common task for web applications is to find out the country/region of a user, based on her IP address, as can be detected in the HTTP request. Depending on the country of origin, the website can translate dates for different time zones, can change locale settings, and, perhaps most commonly, show advertisements in her native language.

To start with, there’s a table which lists the IP ranges per country/region. Let’s assume we’re only dealing with IPv4:

>     CREATE TABLE regions_ip_range (
>       regions_ip_range_id INT UNSIGNED AUTO_INCREMENT,
>       country VARCHAR(64) CHARSET utf8,
>       region VARCHAR(64) CHARSET utf8,
>       start_ip INT UNSIGNED,
>       end_ip INT UNSIGNED,
>       …
>       PRIMARY KEY(regions_ip_range_id),
>       ...
>     );

The table is fixed, and is populated. Now the question arises: how do we query this table, and which indexes should be created?

#### The wrong way

The form I’ve encountered is as follows: an index is declared on regions_ip_range:

>     KEY ip_range_idx (start_ip, end_ip)

And the query goes like this:

>     SELECT * FROM regions_ip_range
>     WHERE my_ip BETWEEN start_ip AND end_ip

Continue reading » “SQL: finding a user’s country/region based on IP”

## Reasons to use innodb_file_per_table

MySQL Configuration InnoDB mysqldump Performance May 21, 2009December 16, 2009

When working with InnoDB, you have two ways for managing the tablespace storage:

1. Throw everything in one big file (optionally split).
2. Have one file per table.

I will discuss the advantages and disadvantages of the two options, and will strive to convince that innodb_file_per_table is preferable.

#### A single tablespace

Having everything in one big file means all tables and indexes, from all schemes, are ‘mixed’ together in that file.

This allows for the following nice property: free space can be shared between different tables and different schemes. Thus, if I purge many rows from my log table, the now unused space can be occupied by new rows of any other table.

This same nice property also translates to a not so nice one: data can be greatly fragmented across the tablespace.

An annoying property of InnoDB’s tablespaces is that they never shrink. So after purging those rows from the log table, the tablespace file (usually ibdata1) still keeps the same storage. It does not release storage to the file system.

I’ve seen more than once how certain tables are left unwatched, growing until disk space reaches 90% and SMS notifications start beeping all around. Continue reading » “Reasons to use innodb_file_per_table”

## The importance of report_host & report_port

MySQL Configuration Replication May 13, 2009May 13, 2009

The two server variables, report_host and report_port, are not commonly used. One of the reasons, I suspect, is that they do not appear in any of the standard .cnf files provided with a MySQL installation.

For a replication environment, I find these two variables to be very useful.

#### Where are these variables used?

Here’s a slightly modified exerpt from a MySQL master node I have, and which has two slaves: Continue reading » “The importance of report_host & report_port”

## MySQL not being able to utilize a compound index?

MySQL Indexing May 7, 2009August 10, 2009

I came today upon a very strange issue. It seems like MySQL is unable to utilize a compound index when evaluating a plan for a query with a range condition. I’m looking for an explanation. I’ll appreciate any insight on this.

Continue reading » “MySQL not being able to utilize a compound index?”