Report abuse

The query


			
insert into places (name,type,parent_id) select w.accent_city,'Feature',p.id from worldcities w join regions_fips r on w.iso_region = r.fips_region join places p on r.fips_region = p.abreviation where p.type = 'Region';

The structure


			
mysql> show create table regions_fips \G
*************************** 1. row ***************************
       Table: regions_fips
Create Table: CREATE TABLE `regions_fips` (
  `iso_country` varchar(2) default NULL,
  `fips_region` varchar(2) default NULL,
  `name` varchar(255) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table places \G
*************************** 1. row ***************************
       Table: places
Create Table: CREATE TABLE `places` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `permalink` varchar(255) default NULL,
  `parent_id` int(11) default NULL,
  `abreviation` varchar(3) default NULL,
  `type` varchar(255) default NULL,
  `is_approved` tinyint(1) NOT NULL default '0',
  `permalinks` varchar(255) default NULL,
  `pictures_permalinks` varchar(255) default NULL,
  `picture_id` int(11) default NULL,
  `pictures_count` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table worldcities \G
*************************** 1. row ***************************
       Table: worldcities
Create Table: CREATE TABLE `worldcities` (
  `iso_country` varchar(2) default NULL,
  `city` varchar(255) default NULL,
  `accent_city` varchar(255) default NULL,
  `iso_region` varchar(2) default NULL,
  `longitude` float default NULL,
  `latitude` float default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Adding the indexes


			
mysql> ALTER TABLE regions_fips ADD INDEX idx_iso ( iso_country );  ALTER TABLE places ADD INDEX idx_abreviation ( abreviation ), ADD INDEX idx_type ( type( 10 ) );
Query OK, 4164 rows affected (0.29 sec)
Records: 4164  Duplicates: 0  Warnings: 0

Query OK, 237299 rows affected (6.23 sec)
Records: 237299  Duplicates: 0  Warnings: 0

Explain


			
mysql> explain select w.accent_city,'Feature',p.id from worldcities w join regions_fips r on w.iso_region = r.fips_region join places p on r.fips_region = p.abreviation where p.type = 'Region' \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: r
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4631
        Extra: 
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: p
         type: ref
possible_keys: idx_abreviation,idx_type
          key: idx_abreviation
      key_len: 12
          ref: pop.r.fips_region
         rows: 1212
        Extra: Using where
*************************** 3. row ***************************
           id: 1
  select_type: SIMPLE
        table: w
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 2593624
        Extra: Using where
3 rows in set (0.00 sec)

The status of the tables


			
mysql> SHOW TABLE STATUS LIKE 'places' \G
*************************** 1. row ***************************
           Name: places
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 238275
 Avg_row_length: 59
    Data_length: 14172160
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: 252808
    Create_time: 2007-05-10 22:03:25
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: InnoDB free: 120832 kB
1 row in set (0.06 sec)

mysql> SHOW TABLE STATUS LIKE 'worldcities' \G
*************************** 1. row ***************************
           Name: worldcities
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 2659856
 Avg_row_length: 65
    Data_length: 174800896
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2007-06-12 18:28:04
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: InnoDB free: 120832 kB
1 row in set (0.06 sec)

mysql> SHOW TABLE STATUS LIKE 'regions_fips' \G
*************************** 1. row ***************************
           Name: regions_fips
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 4417
 Avg_row_length: 48
    Data_length: 212992
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2007-06-12 18:28:04
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: InnoDB free: 120832 kB
1 row in set (0.00 sec)

my.cnf


			
# /etc/mysql/my.cnf: The global mysql configuration file.
# $Header: /var/cvsroot/gentoo-x86/dev-db/mysql/files/my.cnf-4.1,v 1.3 2006/05/05 19:51:40 chtekk Exp $

# The following options will be passed to all MySQL clients
[client]
#password					= your_password
port						= 3306
socket						= /var/run/mysqld/mysqld.sock

[mysql]
character-sets-dir=/usr/share/mysql/charsets
default-character-set=utf8

[mysqladmin]
character-sets-dir=/usr/share/mysql/charsets
default-character-set=utf8

[mysqlcheck]
character-sets-dir=/usr/share/mysql/charsets
default-character-set=utf8

[mysqldump]
character-sets-dir=/usr/share/mysql/charsets
default-character-set=utf8

[mysqlimport]
character-sets-dir=/usr/share/mysql/charsets
default-character-set=utf8

[mysqlshow]
character-sets-dir=/usr/share/mysql/charsets
default-character-set=utf8

[myisamchk]
character-sets-dir=/usr/share/mysql/charsets

[myisampack]
character-sets-dir=/usr/share/mysql/charsets

# use [safe_mysqld] with mysql-3
[mysqld_safe]
err-log						= /var/log/mysql/mysql.err

# add a section [mysqld-4.1] or [mysqld-5.0] for specific configurations
[mysqld]
character-set-server		= utf8
default-character-set		= utf8
user 						= mysql
port 						= 3306
socket 						= /var/run/mysqld/mysqld.sock
pid-file 					= /var/run/mysqld/mysqld.pid
log-error 					= /var/log/mysql/mysqld.err
basedir 					= /usr
datadir 					= /var/lib/mysql
skip-locking
key_buffer 					= 16M
max_allowed_packet 			= 1M
table_cache 				= 64
sort_buffer_size 			= 512K
net_buffer_length 			= 8K
read_buffer_size 			= 256K
read_rnd_buffer_size 		= 512K
myisam_sort_buffer_size 	= 8M
language 					= /usr/share/mysql/english

# security:
# using "localhost" in connects uses sockets by default
skip-networking
bind-address				= 127.0.0.1

log-bin
server-id 					= 1

# point the following paths to different dedicated disks
tmpdir 						= /tmp/
#log-update 				= /path-to-dedicated-directory/hostname

# you need the debug USE flag enabled to use the following directives,
# if needed, uncomment them, start the server and issue 
# #tail -f /tmp/mysqld.sql /tmp/mysqld.trace
# this will show you *exactly* what's happening in your server ;)

#log						= /tmp/mysqld.sql
#gdb
#debug						= d:t:i:o,/tmp/mysqld.trace
#one-thread

# uncomment the following directives if you are using BDB tables
#bdb_cache_size				= 4M
#bdb_max_lock				= 10000

# the following is the InnoDB configuration
# if you wish to disable innodb instead
# uncomment just the next line
#skip-innodb
#
# the rest of the innodb config follows:
# don't eat too much memory, we're trying to be safe on 64Mb boxes
# you might want to bump this up a bit on boxes with more RAM
innodb_buffer_pool_size = 32M
# this is the default, increase it if you have lots of tables
innodb_additional_mem_pool_size = 2M
#
# i'd like to use /var/lib/mysql/innodb, but that is seen as a database :-(
# and upstream wants things to be under /var/lib/mysql/, so that's the route
# we have to take for the moment
#innodb_data_home_dir		= /var/lib/mysql/
#innodb_log_arch_dir		= /var/lib/mysql/
#innodb_log_group_home_dir	= /var/lib/mysql/
# you may wish to change this size to be more suitable for your system
# the max is there to avoid run-away growth on your machine
innodb_data_file_path = ibdata1:10M:autoextend
# we keep this at around 25% of of innodb_buffer_pool_size
# sensible values range from 1MB to (1/innodb_log_files_in_group*innodb_buffer_pool_size)
innodb_log_file_size = 5M
# this is the default, increase it if you have very large transactions going on
innodb_log_buffer_size = 8M
# this is the default and won't hurt you
# you shouldn't need to tweak it
set-variable = innodb_log_files_in_group=2
# see the innodb config docs, the other options are not always safe
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet 			= 16M

[mysql]
# uncomment the next directive if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer 					= 20M
sort_buffer_size 			= 20M
read_buffer 				= 2M
write_buffer 				= 2M

[myisamchk]
key_buffer 					= 20M
sort_buffer_size 			= 20M
read_buffer 				= 2M
write_buffer 				= 2M

[mysqlhotcopy]
interactive-timeout