免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: robert_wjj
打印 上一主题 下一主题

请教mysql倒着查询怎么做 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2014-09-01 17:15 |只看该作者
不知道哇,我再换回innodb试试

论坛徽章:
1
综合交流区版块每日发帖之星
日期:2016-02-13 06:20:00
22 [报告]
发表于 2014-09-02 19:17 |只看该作者
SELECT * FROM table_name ORDER BY ID DESC LIMIT 100;

论坛徽章:
0
23 [报告]
发表于 2014-09-03 15:22 |只看该作者
试了下innodb和myisam都是可以用到索引的。不知道为什么memory不行,真是见鬼了。。

论坛徽章:
1
综合交流区版块每日发帖之星
日期:2016-02-13 06:20:00
24 [报告]
发表于 2014-09-04 16:28 |只看该作者
MariaDB 10.0.10

1).
CREATE TABLE test_ind ( ID BIGINT(16) primary key NOT NULL auto_increment                 
                                      , nTime INT(12)                 
                                      , nSide INT(12)                 
                                      ) engine=memeory;

2).
show create table test_ind;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                             |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_ind | CREATE TABLE `test_ind` (
  `ID` bigint(16) NOT NULL AUTO_INCREMENT,
  `nTime` int(12) DEFAULT NULL,
  `nSide` int(12) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=12702419 DEFAULT CHARSET=latin1 |

MyISAM 而不是 Memory!!!  不知道原因.

3).
insert 12m rows
select count(*) from test_ind;
+----------+
| count(*) |
+----------+
| 12702418 |
+----------+
1 row in set (0.08 sec)

4).
explain select * from test_ind order by id desc limit 100;
+------+-------------+----------+-------+---------------+---------+---------+------+------+-------+
| id   | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | Extra |
+------+-------------+----------+-------+---------------+---------+---------+------+------+-------+
|    1 | SIMPLE      | test_ind | index | NULL          | PRIMARY | 8       | NULL |  100 |       |
+------+-------------+----------+-------+---------------+---------+---------+------+------+-------+

5).
select * from test_ind order by id desc limit 100;
+----------+-------+-------+
| ID       | nTime | nSide |
+----------+-------+-------+
| 12702418 |   479 |   804 |
| 12702417 |   390 |  1530 |
| 12702416 |  1104 |   140 |
| 12702415 |   952 |   792 |
| 12702414 |  2221 |   323 |
| 12702413 |   916 |  1594 |
| 12702412 |  2353 |   995 |
| 12702411 |  1198 |   589 |
| 12702410 |   601 |  1800 |
| 12702409 |  1801 |  2401 |
| 12702408 |  1735 |   201 |
| 12702407 |  2461 |  1824 |
| 12702406 |   337 |  2494 |
| 12702405 |  2103 |   729 |
| 12702404 |  2735 |   262 |
| 12702403 |   207 |  2472 |
| 12702402 |  1133 |  2521 |
| 12702401 |  1369 |  1048 |
| 12702400 |  1096 |   931 |
| 12702399 |  1161 |  1516 |
| 12702398 |  2663 |  2430 |
| 12702397 |  2826 |  1628 |
| 12702396 |   335 |   166 |
| 12702395 |  1727 |   503 |
| 12702394 |   608 |   710 |
| 12702393 |   757 |  1776 |
| 12702392 |   531 |  1670 |
| 12702391 |  2703 |  2328 |
| 12702390 |   313 |   729 |
| 12702389 |   694 |  2279 |
| 12702388 |  1763 |  2397 |
| 12702387 |   979 |  2140 |
| 12702386 |   203 |   918 |
| 12702385 |  1986 |    32 |
| 12702384 |  1502 |  1299 |
| 12702383 |   617 |    71 |
| 12702382 |  2135 |  2005 |
| 12702381 |   105 |  2890 |
| 12702380 |  2651 |  2212 |
| 12702379 |  1251 |   681 |
| 12702378 |    14 |   859 |
| 12702377 |   223 |   736 |
| 12702376 |   468 |  2125 |
| 12702375 |   629 |  1071 |
| 12702374 |  1276 |  2691 |
| 12702373 |  1622 |  1229 |
| 12702372 |   283 |  2294 |
| 12702371 |  2750 |  1707 |
| 12702370 |  1440 |  2014 |
| 12702369 |  2068 |  2729 |
| 12702368 |   872 |  1537 |
| 12702367 |  2610 |  2940 |
| 12702366 |  2745 |   369 |
| 12702365 |  2839 |   452 |
| 12702364 |   688 |  1580 |
| 12702363 |  1469 |  1619 |
| 12702362 |  2024 |   908 |
| 12702361 |  2776 |  2071 |
| 12702360 |  2969 |  2937 |
| 12702359 |  1625 |   969 |
| 12702358 |  1767 |  1385 |
| 12702357 |  1883 |  1483 |
| 12702356 |   444 |   643 |
| 12702355 |  1395 |  1526 |
| 12702354 |  2228 |  2816 |
| 12702353 |  1881 |  2774 |
| 12702352 |    56 |  2210 |
| 12702351 |   909 |  2357 |
| 12702350 |  2246 |  2730 |
| 12702349 |  1308 |  1834 |
| 12702348 |  2510 |   568 |
| 12702347 |   487 |  1995 |
| 12702346 |  2416 |  2147 |
| 12702345 |  2379 |  1311 |
| 12702344 |    87 |   528 |
| 12702343 |  1585 |   969 |
| 12702342 |  1336 |  2319 |
| 12702341 |  1644 |  2454 |
| 12702340 |   655 |  1922 |
| 12702339 |  1867 |  2452 |
| 12702338 |  2869 |   295 |
| 12702337 |  2848 |  1683 |
| 12702336 |  2694 |  2186 |
| 12702335 |  1703 |  2761 |
| 12702334 |  1629 |  1918 |
| 12702333 |  1249 |  1075 |
| 12702332 |   788 |   723 |
| 12702331 |  1857 |    72 |
| 12702330 |  1833 |  2071 |
| 12702329 |  2330 |  2364 |
| 12702328 |  2381 |    94 |
| 12702327 |   766 |   936 |
| 12702326 |   353 |   965 |
| 12702325 |   659 |  2266 |
| 12702324 |  1686 |  2343 |
| 12702323 |   485 |    28 |
| 12702322 |   835 |  1798 |
| 12702321 |  2042 |   793 |
| 12702320 |  1219 |  1140 |
| 12702319 |  2012 |  2651 |
+----------+-------+-------+
100 rows in set (0.05 sec)



论坛徽章:
1
综合交流区版块每日发帖之星
日期:2016-02-13 06:20:00
25 [报告]
发表于 2014-09-04 16:29 |只看该作者
本帖最后由 rhino11 于 2014-09-04 16:37 编辑

对不起.  俺的错.  

CREATE TABLE test_ind ( ID BIGINT(16) primary key NOT NULL auto_increment                 
                                      , nTime INT(12)                 
                                      , nSide INT(12)                 
                                      ) engine=memory;


explain select * from test_ind order by id desc limit 100;                                                      
+------+-------------+----------+------+---------------+------+---------+------+--------+----------------+
| id   | select_type | table    | type | possible_keys | key  | key_len | ref  | rows   | Extra          |
+------+-------------+----------+------+---------------+------+---------+------+--------+----------------+
|    1 | SIMPLE      | test_ind | ALL  | NULL          | NULL | NULL    | NULL | 196608 | Using filesort |
+------+-------------+----------+------+---------------+------+---------+------+--------+----------------+

Memory Engine 俺没经验.

俺想: 由于DATA IN MEMORY,很快, 没必要用INDEX

论坛徽章:
0
26 [报告]
发表于 2014-09-05 11:40 |只看该作者
select * from my_table limit 100,100;   第一个100, 好像是从100开始的记录(位置),后面的那个100是100条记录(数量),  你要查的是101到200的记录对不对? 不知道我理解是否有误。

论坛徽章:
0
27 [报告]
发表于 2014-09-05 11:41 |只看该作者
回复 26# dallpanfh


    sorry 你需要的是最后100行。

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-12-09 06:20:00
28 [报告]
发表于 2014-10-22 16:17 |只看该作者
select last n * from table
where condtion....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP