mysql - sql问题请教
高洛峰
高洛峰 2017-04-17 12:59:54
0
1
471

select a.,b from count a, user b where a.aid = b.aid order by b.count desc limit 10

在count字段中排名前10的字段,找出aid相同的另外一个表的所有字段。
这句能实现,但效率太低,几万记录查询都几秒了。有简单的办法吗

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
迷茫

如果你是是用Mysql,我觉得可以参考下这里的内容
https://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

To increase ORDER BY speed, check whether you can get MySQL to use indexes rather than an extra sorting phase. If this is not possible, you can try the following strategies:

Increase the sort_buffer_size variable value.

Increase the read_rnd_buffer_size variable value.

Use less RAM per row by declaring columns only as large as they need to be to hold the values stored in them. For example, CHAR(16) is better than CHAR(200) if values never exceed 16 characters.

Change the tmpdir system variable to point to a dedicated file system with large amounts of free space. The variable value can list several paths that are used in round-robin fashion; you can use this feature to spread the load across several directories. Paths should be separated by colon characters (“:”) on Unix and semicolon characters (“;”) on Windows, NetWare, and OS/2. The paths should name directories in file systems located on different physical disks, not different partitions on the same disk.

除此之外,也可以试试复合索引,具体可以看看这里
http://www.cnblogs.com/anywei/archive/2011/12/12/mysql.html

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!