欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

mysql操作类,v0.2,增加应对大数据量的一些解决方案

程序员文章站 2024-04-05 16:25:30
...
/*mysql 简单类 by joffe q89949401 围脖@狂code诗人; 本类全静态 使用的时候直接include后 用mysql::方法()名即可 由于类在php5里面全局可视,所以不必担心变量范围的问题.如果有什么意见 请围脖私信||qq邮件; 目前没有与存储过程有关的方法,当然存储过程一般是创建数据库的时候做的. config文件需要配置以下常量信息: LIB:类存放位置 DEBUG:是否开启debug(如果开启会输出错误信息跟踪) TB_EX:数据库表前缀;

增加了select del update insert big_select big_del函数

*/
  1. /*mysql 简单类 by joffe q89949401 围脖@狂code诗人;
  2. 本类全静态 使用的时候直接include后 用mysql::方法()名即可 由于类在php5里面全局可视,所以不必担心变量范围的问题.如果有什么意见 请围脖私信||qq邮件;
  3. 目前没有与存储过程有关的方法,当然存储过程一般是创建数据库的时候做的.
  4. config文件需要配置以下常量信息:
  5. LIB:类存放位置
  6. DEBUG:是否开启debug(如果开启会输出错误信息跟踪)
  7. TB_EX:数据库表前缀;
  8. */
  9. defined('LIB') or die('Missing config!!');
  10. final class mysql {
  11. /**
  12. * 查询总次数
  13. *
  14. * @var int
  15. */
  16. public static $querynum = 0;
  17. /**
  18. * 连接句柄
  19. *
  20. * @var object
  21. */
  22. public static $link;
  23. /*
  24. 表前缀
  25. @var string 下面方法需要在配置文件中配置TB_EX 作为表的前缀
  26. */
  27. static function add_ex($tb){
  28. return TB_EX.$tb_ex.$tb;
  29. }
  30. /*mysql数据库是否使用严格的类型(mysql类型没开启自动转换)默认为false,表示mysql有开启类型转换,这个变量目前只要是用于insert函数的单引号在没有自动转换的mysql里面的问题,可能将来会增加相关函数
  31. */
  32. public static $is_type_tight=false;
  33. /**
  34. * 构造函数
  35. *
  36. * @param string $dbhost 主机名
  37. * @param string $dbuser 用户
  38. * @param string $dbpw 密码
  39. * @param string $dbname 数据库名
  40. * @param int $pconnect 是否持续连接
  41. */
  42. static function connent($dbhost, $dbuser, $dbpw, $dbname = "",$dbcharset, $pconnect = 0) {
  43. if($pconnect) {
  44. if(!self::$link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  45. self::halt("Can not connect to MySQL server");
  46. }
  47. } else {
  48. if(!self::$link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
  49. self::halt("Can not connect to MySQL server");
  50. }
  51. }
  52. if(self::version() > "4.1") {
  53. if($dbcharset) {
  54. mysql_query("SET character_set_connection={$dbcharset}, character_set_results=$dbcharset, character_set_client=binary", self::$link);
  55. }
  56. if(self::version() > "5.0.1") {
  57. mysql_query("SET sql_mode=''", self::$link);
  58. }
  59. }
  60. if($dbname) {
  61. mysql_select_db($dbname, self::$link);
  62. }
  63. }
  64. /**
  65. * 选择数据库
  66. *
  67. * @param string $dbname
  68. * @return
  69. */
  70. static function select_db($dbname) {
  71. return mysql_select_db($dbname, self::$link);
  72. }
  73. /**
  74. * 取出结果集中一条记录
  75. *
  76. * @param object $query
  77. * @param int $result_type
  78. * @return array
  79. */
  80. static function fetch_array($query, $result_type = MYSQL_ASSOC) { //默认只取关联数组 不取数字数组.
  81. return mysql_fetch_array($query, $result_type);
  82. }
  83. /**
  84. * 查询SQL
  85. *
  86. * @param string $sql
  87. * @param string $type
  88. * @return object
  89. */
  90. static function query($sql, $type = "") {
  91. $func = $type == "UNBUFFERED" && @function_exists("mysql_unbuffered_query") ?
  92. "mysql_unbuffered_query" : "mysql_query";
  93. if(!($query = $func($sql, self::$link)) && $type != "SILENT") {
  94. self::halt("MySQL Query Error", $sql);
  95. }
  96. self::$querynum++;
  97. return $query;
  98. }
  99. /**
  100. * 取影响条数
  101. *
  102. * @return int
  103. */
  104. static function affected_rows() {
  105. return mysql_affected_rows(self::$link);
  106. }
  107. /**
  108. * 返回错误信息
  109. *
  110. * @return array
  111. */
  112. static function error() {
  113. return ((self::$link) ? mysql_error(self::$link) : mysql_error());
  114. }
  115. /**
  116. * 返回错误代码
  117. *
  118. * @return int
  119. */
  120. static function errno() {
  121. return intval((self::$link) ? mysql_errno(self::$link) : mysql_errno());
  122. }
  123. /**
  124. * 返回查询结果
  125. *
  126. * @param object $query
  127. * @param string $row
  128. * @return mixed
  129. */
  130. static function result($query, $row,$flname=0) {
  131. $query = @mysql_result($query, $row,$flname);
  132. return $query;
  133. }
  134. /**
  135. * 结果条数
  136. *
  137. * @param object $query
  138. * @return int
  139. */
  140. static function num_rows($query) {
  141. $query = mysql_num_rows($query);
  142. return $query;
  143. }
  144. /**
  145. * 取字段总数
  146. *
  147. * @param object $query
  148. * @return int
  149. */
  150. static function num_fields($query) {
  151. return mysql_num_fields($query);
  152. }
  153. /**
  154. * 释放结果集
  155. *
  156. * @param object $query
  157. * @return bool
  158. */
  159. static function free_result($query) {
  160. return @mysql_free_result($query);
  161. }
  162. /**
  163. * 返回自增ID
  164. *
  165. * @return int
  166. */
  167. static function insert_id() {
  168. return ($id = mysql_insert_id(self::$link)) >= 0 ? $id : self::$result(self::$query("SELECT last_insert_id()"), 0);
  169. }
  170. /**
  171. * 从结果集中取得一行作为枚举数组
  172. *
  173. * @param object $query
  174. * @return array
  175. */
  176. static function fetch_row($query) {
  177. $query = mysql_fetch_row($query);
  178. return $query;
  179. }
  180. /**
  181. * 从结果集中取得列信息并作为对象返回
  182. *
  183. * @param object $query
  184. * @return object
  185. */
  186. static function fetch_fields($query) {
  187. return mysql_fetch_field($query);
  188. }
  189. static function select_affectedt_rows($rs){
  190. return mysql_affected_rows($rs,self::$link);
  191. }
  192. /**
  193. * 返回mysql版本
  194. *
  195. * @return string
  196. */
  197. static function version() {
  198. return mysql_get_server_info(self::$link);
  199. }
  200. /**
  201. * 关闭连接
  202. *
  203. * @return bool
  204. */
  205. static function close() {
  206. return mysql_close(self::$link);
  207. }
  208. /**
  209. * 输出错误信息
  210. *
  211. * @param string $message
  212. * @param string $sql
  213. */
  214. static function halt($message = "", $sql = "") {
  215. @header("Content-type: text/html; charset=utf-8");
  216. if (DEBUG==1){
  217. $debug = debug_backtrace();
  218. echo $message . "\r\n
    SQL--> " . $sql."\r\n
    ERROR_MESSAGE-->".self::error().
  219. "\r\n
    --------------debug--------------\r\n
    ";
  220. self::echoarray($debug);
  221. echo "\r\n
    -------------debug end----------------";
  222. }else{
  223. echo 'SQL Error';
  224. }
  225. @self::rollback();
  226. exit;
  227. }
  228. /////////////////////////////以下是扩展的sql方法.//////
  229. /* 把数组按照 key value value 的对应关系插入数据表table中
  230. table 要插入的数据表
  231. 要注意 这些扩展方法是没自己给表有加前缀的.
  232. */
  233. static function insert($table,$array){
  234. $temp="";$temp2='';
  235. foreach($array as $key=>$value){
  236. if(self::$is_type_tight){
  237. if(is_string($value)){
  238. $temp .="$key,";$temp2 .="'$value',";
  239. }elseif(is_int($value||is_null($value)||is_float($value))){
  240. $value+=0;
  241. $temp .="$key,";$temp2 .="'$value',";
  242. }
  243. }else{
  244. $temp .="$key,";$temp2 .="'$value',";
  245. }
  246. }
  247. $temp = substr($temp,0,strlen($temp)-1);
  248. $temp2 = substr($temp2,0,strlen($temp2)-1);
  249. $sql = "INSERT INTO $table ($temp) VALUE($temp2)";
  250. return self::query($sql);
  251. }
  252. static function del($table,$where){
  253. $sql = "DELETE FROM {$table} where {$where}";
  254. return self::query($sql);
  255. }
  256. static function update($table,$array,$where){
  257. foreach ($array as $key=>$value){
  258. $temp .= "$key='$value',";
  259. }
  260. $temp = substr($temp,0,strlen($temp)-1);
  261. $sql = "update {$table} set ($temp) where {$where} ";
  262. return self::query($sql);
  263. }
  264. /*进行数据库查询select 参数不定
  265. 参数说明:所有参数必须是string
  266. 第一个参数必须是表名;
  267. 从第二个参数起,
  268. 如果是写上 "where:XXX" 则认为是where条件;
  269. 如果写上 "xxx" 则认为是键值
  270. 如果写上 "by:XXX" 则认为是排序
  271. 如果写上 "limit:xxx,xxx" 则认为是分页
  272. #参数不正确则返回false; 成功查询返回查询后的数组;
  273. */
  274. static function select(){
  275. $numargs = func_num_args();//获取参数个数;
  276. $where = "";$key="";$limit="";$by="";
  277. if($numargs==0){return false;}
  278. //echo $numargs;
  279. if($numargs>=2){
  280. $arg_list = func_get_args();
  281. $table = $arg_list[0];
  282. unset($arg_list[0]);
  283. // print_r($arg_list);
  284. foreach($arg_list as $k=>$value){
  285. if(preg_match("#^(where:)\w#",$value)){
  286. $temp = explode(":",$value);
  287. $where = "WHERE {$temp[1]} " ;
  288. }elseif(preg_match("#^by:\w#",$value)){
  289. $temp = explode(":",$value);
  290. $by = "order by {$temp[1]}" ;
  291. }elseif(preg_match("#^limit:\w#",$value)){
  292. $temp = explode(":",$value);
  293. $limit = "limit {$temp[1]}";
  294. }else{
  295. $key .= "$value,";
  296. }
  297. }
  298. if($key==""){
  299. $key = "*";
  300. }else{
  301. $key =substr($key,0,strlen($key)-1);
  302. }
  303. $sql_base="SELECT $key FROM $table";
  304. }
  305. if(!empty($where)){
  306. $sql_base .= " $where";
  307. }
  308. if(!empty($by)){
  309. $sql_base .= " $by";
  310. }
  311. if(!empty($limit)){
  312. $sql_base .= " $limit";
  313. }
  314. //echo $sql_base;
  315. //echo $by ;
  316. $rs = self::query($sql_base);
  317. $re=array();
  318. if(self::num_rows($rs)>=1){
  319. while($info = self::fetch_array($rs)){
  320. $re[]=$info;
  321. }
  322. }
  323. self::free_result($rs);
  324. return $re;
  325. }
  326. /*回滚事务*/
  327. static function rollback(){
  328. self::query('rollback');
  329. }
  330. /*开始事务*/
  331. static function begin(){
  332. self::query('SET AUTOCOMMIT =0'); //停用自动提交;
  333. self::query('BEGIN') ;//开始一个事务;
  334. }
  335. /*提交事务*/
  336. static function commit(){
  337. self::query('commit');
  338. }
  339. static function echoarray($array){
  340. foreach($array as $k=>$v ){
  341. if(is_array($v)){
  342. if(is_array($v)){
  343. echo "
    --------------------------------
    ";
  344. self::echoarray($v);
  345. }
  346. }else{
  347. if($k==='line')
  348. echo "$k -> " .$v." ";
  349. else
  350. echo "$k -> " .$v." ";
  351. }
  352. }
  353. }
  354. function get_server_info(){
  355. return mysql_get_server_info();
  356. }
  357. //下面是应付大数据的表的优化查询
  358. /*
  359. big_select 适合用于大规模的查询,利用覆盖索引实现大幅度的偏移活动窗口,令查询在覆盖索引上偏移而不是在所有数据上,减少msql在数据上检查,再把其他数据join进来这样更效率.但对于小规模数据,这种查询反而增加复杂度,增加优化器压力.说个例子,如果你是limit 10000,20;mysql会先查出10020条数据 再丢弃10000 这种操作代价非常大,利用这个函数可以有效提升效率,但如果是 limit 20,那就会稍微比直接select 慢了一些
  360. @table string要查询的表 如"table1"
  361. @keys string 要查询的键值,多个键值用","分割 如"key1,key2,key3"结束不含","尽量少用"*"且一些关键词请加上`;
  362. @Index string主索引键或者唯一索引键名,只需要一个 如"id";
  363. @pagesize int 分页大小,必须,你不会想这么多数据全部出来吧
  364. @pageNo 页码,从0开始
  365. @orderby string 排序 如"id desc";可空,但不建议空
  366. @where string 条件 如 "date>122424533"可空
  367. #返回数组
  368. */
  369. static function big_select($table,$keys,$index,$pagesize,$pageNo,$orderby=NULL,$where=NULL){
  370. $start=$pageNo*$pagesize;
  371. if($where){
  372. $sqlIndex="SELEECT {$index} from {$table} where {$where}";
  373. }else{
  374. $sqlIndex="SELEECT {$index} from {$table}";
  375. }
  376. if($orderby){
  377. $sqlIndex .=" ORDER BY {$orderby} Limit $start,$pagesize";
  378. }else{
  379. $sqlIndex .=" ORDER BY Limit $start,$pagesize";
  380. }
  381. $sql = "SELECT $keys FROM {$table} INNER JOIN({$sqlIndex}) AS lim USING({$index})";
  382. $rs = self::query($sql);
  383. $re=array();
  384. if(self::num_rows($rs)>=1){
  385. while($info = self::fetch_array($rs)){
  386. $re[]=$info;
  387. }
  388. }
  389. self::free_result($rs);
  390. return $re;
  391. }
  392. /* 如果一个很大的数据(预计大于万行)删除它的工作周期会比较长,会长时间锁住不应该锁住的表或行,令一些不该打断的数据被打断 以下方法是把一个大的sql任务分小(分为一次5000行)但次操作可能会造成删除空隙期间插入了新的数据,而新的数据可能因满足条件而被删除.本方法容易因为超时而失败.
  393. @table string 要删除数据的表名
  394. @where string 条件 可省略
  395. #int 删除掉的行数
  396. */
  397. static function big_del($table,$where){
  398. set_time_limit(0);
  399. $sql="delete from {$table} where {$where} Limit 5000";
  400. $rows = 0;
  401. $eff=0;
  402. do{
  403. self::query($sql);
  404. $rows=self::affected_rows();
  405. $eff += $rows;
  406. }while($rows>0);
  407. return $eff;
  408. }
  409. }
  410. ?>
复制代码