我有以下简单的脚本,它将创建一个包含多个表的数据库,并使用美国的所有zipcodes填充它们。我已经包含了脚本和sql文件,但是,我不是要求任何人查看脚本,而是将其包含在内以供参考。
在我的远程VPS上运行大约需要90秒,而我家的开发箱上大约需要90秒。
突然间,它现在几乎永远在我的VPS上。
除了VPS运行mysql5.1.71且主页框运行mysql5.5之外,两台服务器的配置几乎相同。
我怎样才能看到PHP和MySQL实时使用了多少内存?
EDIT。附上了截图:
error_reporting(E_ALL);
date_default_timezone_set('America/Los_Angeles');
set_time_limit(2000);
$conn = new PDO("mysql:host=localhost;dbname=test_database;charset=utf8",'xxx','xxx',array(PDO::ATTR_EMULATE_PREPARES=>false,PDO::MYSQL_ATTR_USE_BUFFERED_QUERY=>true,PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC));
syslog(LOG_INFO,'Start');
$time1=microtime(true);
shell_exec("mysql -xxx-xxx-hlocalhost -Dtest_database < test_database.sql");
$time2=microtime(true);
$time=$time2-$time1;
echo($time.'<br>');
syslog(LOG_INFO,'Database Built: '.$time);
$file = fopen('zipcodes.csv', "r");
$sql ='INSERT INTO cities (id, name, states_id) VALUES (NULL,?,?)';
$stmt_cities = $conn->prepare($sql);
$sql ='INSERT INTO counties (id, name) VALUES (NULL,?)';
$stmt_counties = $conn->prepare($sql);
$sql ='INSERT INTO zipcodes (id, latitude, longitude, zipcode_types_id, timezones_id, counties_id) VALUES (?,?,?,?,?,?)';
$stmt_zipcodes = $conn->prepare($sql);
$sql ='INSERT INTO cities_has_zipcodes (cities_id, zipcodes_id) VALUES (?,?)';
$stmt_zipcodes_cross = $conn->prepare($sql);
$sql ='SELECT co.id FROM counties AS co INNER JOIN zipcodes AS z ON co.id=z.counties_id INNER JOIN cities_has_zipcodes AS chz ON chz.zipcodes_id=z.id INNER JOIN cities AS c ON c.id=chz.cities_id WHERE co.name=? AND c.states_id=?';
$stmt_check_county = $conn->prepare($sql);
$sql ='SELECT id FROM cities WHERE name=? AND states_id=?';
$stmt_check_city = $conn->prepare($sql);
while (($data = fgetcsv($file, 100000, ",")) !== FALSE)
{
//See if a county of a given name has already been inserted for given state
$stmt_check_county->execute(array($data[5],$data[4]));
if(!$county_id=$stmt_check_county->fetchColumn())
{
$stmt_counties->execute(array($data[5]));
$county_id=$conn->lastInsertId();
}
//See if a city of a given name has already been inserted for given state
$stmt_check_city->execute(array($data[3],$data[4]));
if(!$cities_id=$stmt_check_city->fetchColumn())
{
$stmt_cities->execute(array($data[3],$data[4]));
$cities_id=$conn->lastInsertId();
}
$stmt_zipcodes->execute(array(sprintf("%05d",$data[0]),$data[1],$data[2],$data[6],$data[7],$county_id));
$stmt_zipcodes_cross->execute(array($cities_id,sprintf("%05d",$data[0])));
}
fclose($file);
$time3=microtime(true);
$time=$time3-$time2;
echo($time.'<br>');
syslog(LOG_INFO,'Done: '.$time);
test_database.sql
DROP DATABASE IF EXISTS test_database; CREATE DATABASE test_database; USE test_database;
CREATE TABLE IF NOT EXISTS states ( id CHAR(2) NOT NULL , name VARCHAR(45) NULL , PRIMARY KEY (id) ) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS cities ( id INT UNSIGNED NOT NULL AUTO_INCREMENT , name VARCHAR(45) NOT NULL , states_id CHAR(2) NOT NULL , PRIMARY KEY (id) , INDEX fk_zipcodes_states1_idx (states_id ASC) , UNIQUE INDEX makeUnique (states_id ASC, name ASC) , CONSTRAINT fk_zipcodes_states1
FOREIGN KEY (states_id )
REFERENCES states (id )
ON DELETE NO ACTION
ON UPDATE NO ACTION) ENGINE = InnoDB PACK_KEYS = 0 ROW_FORMAT = DEFAULT;
CREATE TABLE IF NOT EXISTS zipcode_types ( id INT UNSIGNED NOT NULL AUTO_INCREMENT , name VARCHAR(45) NULL , PRIMARY KEY (id) ) ENGINE
= InnoDB PACK_KEYS = 0 ROW_FORMAT = DEFAULT;
CREATE TABLE IF NOT EXISTS counties ( id INT UNSIGNED NOT NULL AUTO_INCREMENT , name VARCHAR(45) NOT NULL , PRIMARY KEY (id) ) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS timezones ( id CHAR(4) NOT NULL , name VARCHAR(45) NOT NULL , PRIMARY KEY (id) ) ENGINE = InnoDB PACK_KEYS
= 0 ROW_FORMAT = DEFAULT;
CREATE TABLE IF NOT EXISTS zipcodes ( id CHAR(5) NOT NULL , longitude DECIMAL(9,6) NOT NULL , latitude DECIMAL(9,6) NOT NULL , zipcode_types_id INT UNSIGNED NOT NULL , counties_id INT UNSIGNED NOT NULL , timezones_id CHAR(4) NOT NULL , PRIMARY KEY (id) , INDEX fk_zipcodes_zipcode_types1_idx (zipcode_types_id ASC) , INDEX fk_zipcodes_counties1_idx (counties_id ASC) , INDEX fk_zipcodes_timezones1_idx (timezones_id ASC) , CONSTRAINT fk_zipcodes_zipcode_types1
FOREIGN KEY (zipcode_types_id )
REFERENCES zipcode_types (id )
ON DELETE NO ACTION
ON UPDATE NO ACTION, CONSTRAINT fk_zipcodes_counties1
FOREIGN KEY (counties_id )
REFERENCES counties (id )
ON DELETE NO ACTION
ON UPDATE NO ACTION, CONSTRAINT fk_zipcodes_timezones1
FOREIGN KEY (timezones_id )
REFERENCES timezones (id )
ON DELETE NO ACTION
ON UPDATE NO ACTION) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS cities_has_zipcodes ( cities_id INT UNSIGNED NOT NULL , zipcodes_id CHAR(5) NOT NULL , PRIMARY KEY (cities_id, zipcodes_id) , INDEX fk_cities_has_zipcodes_zipcodes1_idx (zipcodes_id ASC) , INDEX fk_cities_has_zipcodes_cities1_idx (cities_id ASC) , CONSTRAINT fk_cities_has_zipcodes_cities1
FOREIGN KEY (cities_id )
REFERENCES cities (id )
ON DELETE NO ACTION
ON UPDATE NO ACTION, CONSTRAINT fk_cities_has_zipcodes_zipcodes1
FOREIGN KEY (zipcodes_id )
REFERENCES zipcodes (id )
ON DELETE NO ACTION
ON UPDATE NO ACTION) ENGINE = InnoDB;
INSERT INTO zipcode_types (id, name) VALUES (1,'Standard'); INSERT INTO zipcode_types (id, name) VALUES (2,'PO Box Only'); INSERT INTO zipcode_types (id, name) VALUES (3,'Military'); INSERT INTO zipcode_types (id, name) VALUES (4,'Unique');
INSERT INTO timezones (id, name) VALUES ('HAST','Hawaii-Aleutian Standard Time'); INSERT INTO timezones (id, name) VALUES ('AKST','Alaska Standard Time'); INSERT INTO timezones (id, name) VALUES ('PST','Pacific Standard Time'); INSERT INTO timezones (id, name) VALUES ('MST','Mountain Standard Time'); INSERT INTO timezones (id, name) VALUES ('CST','Central Standard Time'); INSERT INTO timezones (id, name) VALUES ('EST','Eastern Standard Time'); INSERT INTO timezones (id, name) VALUES ('AST','Atlantic Standard Time'); INSERT INTO timezones (id, name) VALUES ('XXX','Unknown');
INSERT INTO states (id,name) VALUES ('AL','ALABAMA'); INSERT INTO states (id,name) VALUES ('AK','ALASKA'); INSERT INTO states (id,name) VALUES ('AS','AMERICAN SAMOA'); INSERT INTO states (id,name) VALUES ('AZ','ARIZONA'); INSERT INTO states (id,name) VALUES ('AR','ARKANSAS'); INSERT INTO states (id,name) VALUES ('CA','CALIFORNIA'); INSERT INTO states (id,name) VALUES ('CO','COLORADO'); INSERT INTO states (id,name) VALUES ('CT','CONNECTICUT'); INSERT INTO states (id,name) VALUES ('DE','DELAWARE'); INSERT INTO states (id,name) VALUES ('DC','DISTRICT OF COLUMBIA'); INSERT INTO states (id,name) VALUES ('FM','FEDERATED STATES OF MICRONESIA'); INSERT INTO states (id,name) VALUES ('FL','FLORIDA'); INSERT INTO states (id,name) VALUES ('GA','GEORGIA'); INSERT INTO states (id,name) VALUES ('GU','GUAM'); INSERT INTO states (id,name) VALUES ('HI','HAWAII'); INSERT INTO states (id,name) VALUES ('ID','IDAHO'); INSERT INTO states (id,name) VALUES ('IL','ILLINOIS'); INSERT INTO states (id,name) VALUES ('IN','INDIANA'); INSERT INTO states (id,name) VALUES ('IA','IOWA'); INSERT INTO states (id,name) VALUES ('KS','KANSAS'); INSERT INTO states (id,name) VALUES ('KY','KENTUCKY'); INSERT INTO states (id,name) VALUES ('LA','LOUISIANA'); INSERT INTO states (id,name) VALUES ('ME','MAINE'); INSERT INTO states (id,name) VALUES ('MH','MARSHALL ISLANDS'); INSERT INTO states (id,name) VALUES ('MD','MARYLAND'); INSERT INTO states (id,name) VALUES ('MA','MASSACHUSETTS'); INSERT INTO states (id,name) VALUES ('MI','MICHIGAN'); INSERT INTO states (id,name) VALUES ('MN','MINNESOTA'); INSERT INTO states (id,name) VALUES ('MS','MISSISSIPPI'); INSERT INTO states (id,name) VALUES ('MO','MISSOURI'); INSERT INTO states (id,name) VALUES ('MT','MONTANA'); INSERT INTO states (id,name) VALUES ('NE','NEBRASKA'); INSERT INTO states (id,name) VALUES ('NV','NEVADA'); INSERT INTO states (id,name) VALUES ('NH','NEW HAMPSHIRE'); INSERT INTO states (id,name) VALUES ('NJ','NEW JERSEY'); INSERT INTO states (id,name) VALUES ('NM','NEW MEXICO'); INSERT INTO states (id,name) VALUES ('NY','NEW YORK'); INSERT INTO states (id,name) VALUES ('NC','NORTH CAROLINA'); INSERT INTO states (id,name) VALUES ('ND','NORTH DAKOTA'); INSERT INTO states (id,name) VALUES ('MP','NORTHERN MARIANA ISLANDS'); INSERT INTO states (id,name) VALUES ('OH','OHIO'); INSERT INTO states (id,name) VALUES ('OK','OKLAHOMA'); INSERT INTO states (id,name) VALUES ('OR','OREGON'); INSERT INTO states (id,name) VALUES ('PW','PALAU'); INSERT INTO states (id,name) VALUES ('PA','PENNSYLVANIA'); INSERT INTO states (id,name) VALUES ('PR','PUERTO RICO'); INSERT INTO states (id,name) VALUES ('RI','RHODE ISLAND'); INSERT INTO states (id,name) VALUES ('SC','SOUTH CAROLINA'); INSERT INTO states (id,name) VALUES ('SD','SOUTH DAKOTA'); INSERT INTO states (id,name) VALUES ('TN','TENNESSEE'); INSERT INTO states (id,name) VALUES ('TX','TEXAS'); INSERT INTO states (id,name) VALUES ('UT','UTAH'); INSERT INTO states (id,name) VALUES ('VT','VERMONT'); INSERT INTO states (id,name) VALUES ('VI','VIRGIN ISLANDS'); INSERT INTO states (id,name) VALUES ('VA','VIRGINIA'); INSERT INTO states (id,name) VALUES ('WA','WASHINGTON'); INSERT INTO states (id,name) VALUES ('WV','WEST VIRGINIA'); INSERT INTO states (id,name) VALUES ('WI','WISCONSIN'); INSERT INTO states (id,name) VALUES ('WY','WYOMING'); INSERT INTO states (id,name) VALUES ('AA','AA UNKNOWN'); INSERT INTO states (id,name) VALUES ('AE','AE UNKNOWN'); INSERT INTO states (id,name) VALUES ('AP','AP UNKNOWN');