mysql 一对多表查询_Mysql多对多查询、列合并

表信息

资源表 synsource                                                 199824       rows

产品表 tab_product_detail                                  108              rows

资源与产品多对多的关联表 tab_r_sourmach  1,113,866   rows以上表均采用MyISAM引擎.

连接测试

因 为方便用户进行更好的资源的搜索,所以需要将资源数据全部建立索引至Lucene中,希望在Lucene中存储的Document为:

sourceNamefileNamesubjectgradeproducts

英语学生用书第十一册外研新标准.nwe英语小学NP7000 NP6000 NP2300

英语学生用书第十二册外研新标准2.nwe英语小学NP2300

以上前四个字段是属于 synsource表中,而 products 属于 tab_product_detail 表,它们之间的关系由tab_r_sourmach进行中间关联。

一开始想到的是直接使用以下SQL:

select s1.sourid, s1.sourcename , t.product_name from synsource s1

left join tab_r_sourmach c

on s1.sourid=c.sourid

left join tab_product_detail t

on c.product_id = t.product_id where s1.sourceid=1

souridsourcenamesubjectnameproduct_name

1小六上Module07英语ND520

1小六上Module07英语NP560T

1小六上Module07英语NP560+

1小六上Module07英语NP360+5 rows in set (0.45 sec)

对于以上SQL的结果重复数据的合并处理考虑采用了 GROUP_CONCAT:

select s1.*, GROUP_CONCAT(t.product_name SEPARATOR ' ') as product_name

from tab_synsource s1

left join tab_r_sourmach c

on s1.sourid=c.sourid

left join tab_product_detail t

on c.product_id = t.product_id where s1.sourceid=1

souridsourcenamesubjectnameproduct_name

1小六上Module07英语ND520 NTV518 NP560T NP560+ NP360+1 row in set (0.31 sec)

看上去似乎能够满足需求,但是以上只是针对单条数据进行查询,对于数据的批量索引建立的话肯定是直接 limit 500之类的操作。于是去掉以上where加上 limit 10执行之后,半天都执行不出来,估计它先会全部数据连接查询之后再去 limit。

于是先测试一下直接(tab_r_sourmach, tab_product_detail)两表关联看看效率如何:

select c.sourid

from tab_r_sourmach c left join tab_product_detail t

on c.product_id = t.product_id

group by c.sourid limit 500;

以上语句用时:(5.65 sec)

再加上GROUP_CONCAT试试:

select c.sourid, GROUP_CONCAT(t.product_name SEPARATOR ' ') as products

from tab_r_sourmach c left join tab_product_detail t

on c.product_id = t.product_id

group by c.sourid limit 800;

输出结果样例:

souridproducts

767ND520 ND416 NTV518 NP560T NP560+ NP360+

760ND550 NP600+ NP9588 NP600

800 rows in set (1.74 sec) , 经过反复测试都发现加上了GROUP_CONCAT 函数比没使用该函数快了五倍多,暂时还不知道为什么。

不过1秒左右对于后台索引建立来说还算能够接受,于是再将以上SQL与 synsource 进行连接测试:

select * from synsource s, (

select c.sourid,

GROUP_CONCAT(t.product_name SEPARATOR ' ')

from tab_r_sourmach c left join tab_product_detail t

on

c.product_id = t.product_id

group by c.sourid limit 1000

) as b

where s.sourid = b.sourid;

输出结果样例:

968词汇学习-第三级K(参考剑桥少儿英语)英语NP600+ NP600

983词汇学习-六年级上Unit7(参考湘教版)英语ND520 ND416 NTV518 NP560T NP560+ NP360+

1000 rows in set (2.02 sec)

不知道大家对这种情况如何处理的?以上的SQL虽然能够达到目的,不过还是存在不足。

忘说了,在使用MYSQL GROUP_CONCAT函数进行分组连接时,它对字符串的长度连接是有长度限止的,默认的情况的话我发布 products 太长的话会被截断,如果想一劳永逸的话直接在my.cnf 中加入 group_concat_max_len=99999 即可。

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-04-11 21:24

浏览 16529

论坛回复 / 浏览 (2 / 3771)

分类:数据库

评论

2 楼

denger

2011-04-15

java_user 写道

问下,那数据说的是诺亚舟点读机?

2fd2d9c1d175d11752585fa9b76aba29.gif

1 楼

java_user

2011-04-15

问下,那数据说的是诺亚舟点读机?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值