function insert(){ global $m; if (!isset($_REQUEST['strs']) || !isset($_REQUEST['type']) || !isset($_REQUEST['hash'])){ echo 'param error'; return; } //strs为所有字符串 $poststr = $_REQUEST['strs']; $xstrs = json_decode(stripslashes($poststr), true); $type = $_REQUEST['type']; $hash = $_REQUEST['hash']; if (count($xstrs) <= 0){ $msg = 'str error'; DsLog::errLog($msg.$poststr); echo $msg; return; } if ($type == '0'){ $table = 'white'; } else if($type == '1'){ $table = 'black'; } else{ $msg = 'type error'; DsLog::errLog($msg); echo $msg; return; } $strs = array(); for($i = 0; $i < count($xstrs); $i++) { $strtmp = $xstrs[$i]; $strtmp = trim($strtmp); $strtmp = strtolower($strtmp); $strtmp = addslashes($strtmp); if (strlen($strtmp) > 256){ $strtmp = substr($strtmp, 0, 256); } if (strlen($strtmp) >= 7) { array_push($strs, $strtmp); } } //拼接所有字符串 $tmp = '("'.implode('","', $strs).'")'; //获取已存在的字符串 $sql = "select * from $table where str in $tmp"; $ret = mysql_query($sql, $m); if (!$ret){ $msg = 'exec error:'.mysql_error($m).','.$sql; DsLog::errLog($msg); echo $msg; return; } $exists = array(); $notexists = array(); $count = mysql_num_rows($ret); for ($i = 0; $i < $count; $i++) { $item = mysql_fetch_assoc($ret); if (!$item){ break; } array_push($exists, $item['str']); } for ($i = 0; $i < count($strs); $i++){ if (in_array($strs[$i], $exists)){ continue; } array_push($notexists, $strs[$i]); } for($i = 0; $i < count($exists); $i++) { $exists[$i] = addslashes($exists[$i]); } for($i = 0; $i < count($notexists); $i++) { $notexists[$i] = addslashes($notexists[$i]); } if (count($exists) > 0){ //更新已存在字符串的count字段 $tmp = '("'.implode('","', $exists).'")'; $time = date('YmdHi'); $sql = "update $table set count=count+1 where str in $tmp"; $ret = mysql_query($sql, $m); if (!$ret){ $msg = 'exec error:'.mysql_error($m).','.$sql; DsLog::errLog($msg); echo $msg; return; } //更新已存在字符串的upd字段 $sql = "update $table set upd='$time' where str in $tmp"; $ret = mysql_query($sql, $m); if (!$ret){ $msg = 'exec error:'.mysql_error($m).','.$sql; DsLog::errLog($msg); echo $msg; return; } } //插入新信息 if (count($notexists) > 0){ $time = date('YmdHi'); $sql = "insert ignore into $table (str,hash,count, upd) values"; for ($i = 0; $i < count($notexists); $i++){ $str = $notexists[$i]; $crc = sprintf("%u", crc32($str)); $sql .= "('$str','$crc','1', '$time'),"; } $sql = substr($sql, 0, strlen($sql) - 1); $ret = mysql_query($sql, $m); if (!$ret){ $msg = 'insert error:'.mysql_error($m).','.$sql; DsLog::errLog($msg); echo $msg; return; } } echo !!$ret; }
我现在要对字符串做统计,统计每个字符串的数量,就是类似一个map, 这符串:数量,当然,数据库顺便还保存了一下字符中的crc等信息
insert函数接收post过来的strs,这是一个字符串数组,每次post过来的字符串数量平均为1500
现在数据库里的记录1200W条,每一次insert花费的时间平均为20秒(经常出现30秒超时)
请问下各位,该怎么做优化,现在只是统计了很小一部分,预计统计完数据量大概在1-10亿条
回复讨论(解决方案)
对了,str字段做了唯一索引,其它字段没做,mysql为MyISAM
你能确定瓶颈是在数据库做 查询 和 修改 的3条语句上吗?
你说 每次post过来的字符串数量平均为1500
那是指 1500 的单词吗?就算不是(是串长度)那么以平均每个单词20个字符计算,也有75个单词
你的过滤条件是 str in $tmp (str in ('xxx','xxx'....))也就是对表中的每一条记录都要做 1500(75)次字符串比较,而命中率至多是1/1500(1/75)你认为这样做合适吗?
数据库的强项在于记录间的比较,而你却在做弱势的列间比较
你可以用传入的数据构造出一个临时表(每行一个单词),再去与主表关联操作。这样才能发挥数据库的优势
再有,你的连续 2 条 update 语句不能合并在一起吗?
你能确定瓶颈是在数据库做 查询 和 修改 的3条语句上吗?
你说 每次post过来的字符串数量平均为1500
那是指 1500 的单词吗?就算不是(是串长度)那么以平均每个单词20个字符计算,也有75个单词
你的过滤条件是 str in $tmp (str in ('xxx','xxx'....))也就是对表中的每一条记录都要做 1500(75)次字符串比较,而命中率至多是1/1500(1/75)你认为这样做合适吗?
数据库的强项在于记录间的比较,而你却在做弱势的列间比较
你可以用传入的数据构造出一个临时表(每行一个单词),再去与主表关联操作。这样才能发挥数据库的优势
再有,你的连续 2 条 update 语句不能合并在一起吗?
1500是指字符数,字符数应该在5W左右
你的意思是先把1500条记录insert到一个临时表tmp_table,然后
然后 select* from table inner join tmp_talbe on table.str = tmp_table.str 这样么?
你能确定瓶颈是在数据库做 查询 和 修改 的3条语句上吗?
你说 每次post过来的字符串数量平均为1500
那是指 1500 的单词吗?就算不是(是串长度)那么以平均每个单词20个字符计算,也有75个单词
你的过滤条件是 str in $tmp (str in ('xxx','xxx'....))也就是对表中的每一条记录都要做 1500(75)次字符串比较,而命中率至多是1/1500(1/75)你认为这样做合适吗?
数据库的强项在于记录间的比较,而你却在做弱势的列间比较
你可以用传入的数据构造出一个临时表(每行一个单词),再去与主表关联操作。这样才能发挥数据库的优势
再有,你的连续 2 条 update 语句不能合并在一起吗?
1500是字符串数,大概3W个字符
你的意思是把1500个字符中插入到临时表tmp里,然后再 select* from white inner join tmp on white.str=tmp.str 么?
我刚测了下
select* from white inner join tmp on white.str=tmp.str 执行时间10S
id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE tmp index str str 770 NULL 2531 Using index1 SIMPLE white eq_ref str str 770 bayes.tmp.str 1
直接执行 select * from white where str in(''xxx','xxx'...) 执行时间9S
id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE white range str str 770 NULL 2531 Using where
感觉效果差不多一样
1、mysql 有内存表,你并未使用
2、将两条 update 合并,应该能减少 10 秒
mysql_error这种库函数的效率也不高吧而且官方也不推荐,我几乎没有用过这种,我学PHP的时候,直接开始PDO的。
还有,你用了in这个关键字,好像它是不走索引的吧。
感觉效果差不多一样
应该是你测试环境的问题吧,联表查询大多数情况下是要比in快的
你测的结果差不多可能是如下原因
1.查询表的数据没有达到一定量
2.两种查询方式查出的结果集不同
3.查询字段未加索引或加的不对
4.被其他索引过多而影响
5.其他原因
我认为比较明显的就是第2个原因
你联表查时没有指定查询字段,联表出的字段肯定要比不连表的要多
还有联表查询会有一对多的情况,那样一条记录就会变成多条的,要加group by

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft