PHP代码:
<!DOCTYPE html><!--STATUS OK-->    <html><head>
<meta http-equiv="content-type" content="text/html;charset=gbk"><style>
table,td{border:1px solid #ccc;}
td{padding:10px}</style></head><?php
$dbh = @mysql_connect("localhost:3306","root","123456");
 
 /* 定义变量dbh , mysql_connect()函数的意思是连接mysql数据库, "@"的意思是屏蔽报错 */
if(!$dbh){die("error");}
/* die()函数的意思是将括号里的字串送到浏览器并中断php程式 (script)。括号里的参数为欲送出的字串。 */
@mysql_select_db("data", $dbh);mysql_query("SET NAMES data");?><br><br><br><center>
<form name="f" method=post action="ss.php">
<input type="text" name="wd" id="kw" maxlength="100">
<input type="hidden" name="rsv_bp" value="0">
<input type="hidden" name="rsv_spt" value="3">
<span class="btn_wr">
<input type="submit" value="搜索一下" id="su" class="btn" style1=width:200px; onmousedown="this.className='btn btn_h'" onmouseout="this.className='btn'"></span></form></center><?
$wd=$_POST["wd"];
/* 选择mysql服务器里的一个数据库,这里选的数据库名为 ok */
$q = "(select * from tabal where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal1 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal2 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal3 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal4 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal5 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal6 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal7 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal8 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal9 where user like '$wd%' or email LIKE '$wd%')
union all
(select * from tabal10 where email LIKE '$wd%')
union all
(select * from tabal11  where user like '$wd%' or email LIKE '$wd%') LIMIT 0,20";/* 定义变量q, "select * from abc"是一个sql语句,意思是读取表abc中的数据 */
?>
<br /><center>
<table width=780><?
$rs = mysql_query($q, $dbh);while($row = mysql_fetch_array($rs))echo "<tr><td width=26%>$row[user]</td> <td  width=25%>$row[pass]</td> <td  width=25%>$row[email]</td></tr> ";?></table></center>
 
<?
@mysql_close($dbh);
/* 关闭到mysql数据库的连接 */
?></html>
我的数据库中有11个表 有uid user pass email 4个字段我每个表都分别给user和email建了索引每个表数据在600万-1000万左右
我在本机用 PHPNOW 搭建的环境 但是只用了里面的MYSQL数据库  PHP用的IIS 没用apache my.ini文件的配置是这样的
# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:\mysql\data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock# Here follows entries for some specific programs# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 512
tmp_table_size = 2048M
sort_buffer_size = 128M
query_cache_size = 1024M
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 32Mmax_connections = 1000
default-storage-engine = MYISAM
skip-innodb
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!

#skip-networking# Disable Federated by default
skip-federated# Replication Master Server (default)
# binary logging is required for replication
##log-bin=mysql-bin# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
#    the syntax is:
#
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
#    where you replace <host>, <user>, <password> by quoted strings and
#    <port> by the master's port number (3306 by default).
#
#    Example:
#
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
#    MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
#    start replication for the first time (even unsuccessfully, for example
#    if you mistyped the password in master-password and the slave fails to
#    connect), the slave will create a master.info file, and any later
#    change in this file to the variables' values below will be ignored and
#    overridden by the content of the master.info file, unless you shutdown
#    the slave server, delete master.info and restart the slaver server.
#    For that reason, you may want to leave the lines below untouched
#    (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id       = 2
#
# The replication master for this slave - required
#master-host     =   <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     =  <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin# Point the following paths to different dedicated disks
#tmpdir = /tmp/
#log-update  = /path-to-dedicated-directory/hostname# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\mysql\data/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = C:\mysql\data/
#innodb_log_arch_dir = C:\mysql\data/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50[mysqldump]
quick
max_allowed_packet = 16M[mysql]
no-auto-rehash
# Remove the next comment character 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在本机用的话 会比较卡 我又搭建了一个2003系统的虚拟机 直接装的phpnow 吧数据库考进去 执行php程序的时候 CPU直接到了100%了 有个system.exe进程 站50%以上 mydql-nt.exe也占三四十的CPU 
情哪位大侠帮忙分析下 是php脚本的问题?是MYSQL.INI的问题?还是数据太多的问题?或是索引不当?还是配置低?我的配置 :CPU G850 2.90Ghz  4G内存 
 

解决方案 »

  1.   

    这点代码按理说不应该卡啊。随便建个test.php 里面写上:
    <?php
       phpinfo(); //测试看看如何
      

  2.   

    一张表600W,算是很大了。可以考虑一下采用merge表引擎来整合。
    另600W*11的数据,还在用集成环境,还在用windows,还只一台机器,还在用LIKE
      

  3.   

    一次就要查询 11 个表,卡是必然的
    建议分别按 user 和 email 创建视图
    查询时也先区分一下是用户名还是邮箱后在做相关查询
      

  4.   

    $q = "(select * from tabal where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal1 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal2 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal3 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal4 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal5 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal6 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal7 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal8 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal9 where user like '$wd%' or email LIKE '$wd%')
    union all
    (select * from tabal10 where email LIKE '$wd%')
    union all
    (select * from tabal11  where user like '$wd%' or email LIKE '$wd%') LIMIT 0,20";优化每张表的查询,关键字段需要加上索引。