用distinct or array_unique

 

 在 Mysql 获取数据时,若是想获取某一列去重数据,若是获取呢php

举个例子:mysql

advert_pro_ad 表sql

CREATE TABLE `advert_pro_ad` (
  `advert_id` int(11) NOT NULL DEFAULT '0' COMMENT '广告id',
  `pro_id` int(11) NOT NULL DEFAULT '0' COMMENT '项目id',
  UNIQUE KEY `uniq_pro_aid` (`advert_id`,`pro_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '广告和广告项目关联表';

 

项目和广告是一对多的关系。如何获取去重以后的项目Id呢?spa

有三种办法code

1,blog

select distinct(pro_id) from advert_pro_ad  order by pro_id desc

2,进程

select pro_id from advert_pro_ad  order by pro_id desc

把数据取出来以后,再用 array_unique 去重内存

3,io

select pro_id from advert_pro_ad group by pro_id order by pro_id desc

 

建议使用第一种,使用第二种会有如下弊端class

一、进程间IO通信暴增。从mysql会向php传大量的数据。IO通信是最影响速度的。
二、内存限制。PHP是内存操做。一般默认执行内存为128M,能处理的数据量只会大大小于128M.
除非改默认设置到较大值,加大内存开销。
三、效率较差。不只从mysql到php有复制,并且array_unique效率也mysql DISTINCT差。

 

 

转自:https://stackoverflow.com/questions/19473869/select-distinct-or-array-unique

相关文章
相关标签/搜索