作者sunlights (sunlights)
看板PHP
标题Re: [请益] 用PHP做运算 还是用MYSQL做运算 ?
时间Sun Nov 4 23:11:08 2012
※ 引述《sunlights (sunlights)》之铭言:
: ※ 引述《jl844032 (湖蓝者)》之铭言:
: : 如题
: : 想请问版上大德们
: : 一个PHP想要从MYSQL拉资料後 再用PHP做运算比较好
: : 还是在MYSQL做完运算後 传回来PHP比较好
: : 关於效能方面 哪个方式会比较好?
: 大家都觉得用MYSQL做运算好..
: 可是我告诉大家一个实情..
: 当资料在3万笔以上..sql的速度会奇惨无比
: 反而是php虽然也是要等待..但比起sql真是好太多了..
: 这是我的经验..
: 所以我觉得..资料库的软体真的尚待努力
当时的环境是mssql
(mysql我没有测过..但是当时资料笔数大概快5万笔..现在己经10几快20万)
A表放的是调查者的姓名和姓别,B放填写统计表的项目(共有20笔复选)
分别以b1,b2,b3...b20个栏位代表,栏位型态tinyint,有选为1,没有选为0
要做出
b1 b2 b3............b20
---------------------------------------
男
女
那时是用
select count(*) from A left join b on A.pid=b.pid where A.sex='F' and b.b1=1
select count(*) from A left join b on A.pid=b.pid where A.sex='M' and b.b1=1
select count(*) from A left join b on A.pid=b.pid where A.sex='F' and b.b2=1
select count(*) from A left join b on A.pid=b.pid where A.sex='M' and b.b2=1
......
select count(*) from A left join b on A.pid=b.pid where A.sex='F' and b.b20=1
select count(*) from A left join b on A.pid=b.pid where A.sex='M' and b.b20=1
pid是帐号
结果跑到快吐血..
後来直接select index 栏位用php去run 才OK
我现在都找不到用sql解决的方法..
欢迎大家尝试..
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.240.25.155
※ 编辑: sunlights 来自: 111.240.25.155 (11/04 23:13)
1F:推 LPH66:5 万笔 * 40 次 query = 总计看过 200 万资料行... 11/04 23:27
2F:→ LPH66:我会想用 select sum(b.b1),sum(b.b2),... 11/04 23:28
3F:→ LPH66:然後出来会得到一笔 20 个栏位的资料就是输出表格的一列 11/04 23:29
4F:→ LPH66:这样只有两次 query 应该会好很多... 11/04 23:29
5F:→ LPH66:这个方法也是考虑到 join 的效率问题才让 join 次数越少越好 11/04 23:30
6F:→ sunlights:但是怎麽关联到A的sex呢?? 11/04 23:33
7F:推 LPH66:跟你一样的 join 啊 只是不用 and b.b1=1 11/04 23:37
8F:→ LPH66:where A.sex='F' 一次 where A.sex='M' 一次 共两次query 11/04 23:37
9F:→ sunlights:很棒..但是如果遇到值不是只有0,1..还有2,3,4...甚至是 11/04 23:41
10F:→ sunlights:文字的话..是否也无解呢?? 11/04 23:41
11F:→ sunlights:对了..虽然我刚说很棒..但我实际去测6万笔资料和6个复选 11/04 23:47
12F:→ sunlights:栏位..只查男生 sex='M' 也要29秒..3万笔资料用17秒跑完 11/04 23:49
13F:→ sunlights:如果是20个复选栏位可能更多.. 11/04 23:50
14F:→ sunlights:如果A表格的查询栏位从sex改成city(台北,台中,高雄..) 11/04 23:51
15F:→ sunlights:可能也会查29X10(假设只有10个城市)=290秒=4.5分 11/04 23:52
16F:→ sunlights:但要统计的选项愈多..就会愈慢..这点php跑的速度就超越 11/04 23:53
17F:→ sunlights:这应该就是inav大的切身之痛吧. 11/04 23:54
18F:推 cjoe:b.pid, A.sex, b.b1等等栏位都有做索引吗? 11/04 23:59
19F:→ sunlights:都要做索引吗??那大概全部的栏位都要加索引了..这样OK吗 11/05 00:25
20F:→ qwertmn:b1 b2 b3...b20..这些栏位都没有关连性ˇˇ 11/05 03:49
21F:→ qwertmn:硬是join 20次- -"" 我会用Union..把资料串接起来.. 11/05 03:52