首页 > 数据库 > PostgreSQL > 错误结果?警惕mysql_fdw数据黑洞
readonly@localhost [test]>show create table data_tbl\G *************************** 1.row *************************** Table: data_tbl CreateTable: CREATE TABLE`data_tbl` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `random_string` longtext NOT NULL, `created_at`timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) readonly@localhost [test]>select count(1) from data_tbl\G *************************** 1.row *************************** count(1): 3 1 row in set (0.00 sec) readonly@localhost [test]>select max(id) from data_tbl\G *************************** 1.row *************************** max(id): 3 1 row inset (0.00 sec) readonly@localhost [test]>select id,random_string from data_tbl order by id desc limit 1\G *************************** 1.row *************************** id: 3 random_string: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ --random_string字段值比较长,这里截取了一部分,可以看到查询的id为3
test=> \d+ data_tbl Foreign table "public.data_tbl" Column | Type | Collation | Nullable | Default | FDW options | Storage | Stats target | Description ---------------+-----------------------------+-----------+----------+---------+-------------+----------+--------------+------------- id | bigint | | | | | plain | | random_string | text | | | | | extended | | created_at | timestamp without time zone | | | | | plain | | Server: mysql_test FDW options: (dbname 'test', table_name 'data_tbl') test=> select count(1) from data_tbl; -[ RECORD 1 ] count | 3 test=> select id,random_string from data_tbl order by id desc limit 1; -[ RECORD 1 ]-+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- id | 1 random_string | ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ --random_string字段值比较长,这里截取了一部分,可以看到查询的id为1
test=> select id from data_tbl order by id desc limit 1; id ---- 3 (1 row) test=>





mysql> select id,length(random_string) from data_tbl; +----+-----------------------+ | id | length(random_string) | +----+-----------------------+ | 1 | 65536 | | 2 | 65537 | | 3 | 65536 | +----+-----------------------+ 3 rows in set (0.01 sec) mysql>
test=> explain (analyze,verbose) select id,random_string from data_tbl order by id desc limit1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------- Limit (cost=1030.00..1030.00 rows=1 width=40) (actual time=10.939..10.941 rows=1 loops=1) Output: id, random_string -> Sort (cost=1030.00..1032.50 rows=1000 width=40) (actual time=10.935..10.936 rows=1 loops=1) Output: id, random_string Sort Key: data_tbl.id DESC Sort Method: quicksort Memory: 89kB -> Foreign Scan on public.data_tbl (cost=25.00..1025.00 rows=1000 width=40) (actual time=10.861..10.866 rows=1 loops=1) Output: id, random_string Remote server startup cost: 25 Remote query: SELECT `id`, `random_string` FROM `test`.`data_tbl` Planning Time: 1.636 ms Execution Time: 13.692 ms (12rows) test=> select id,random_string from data_tbl where id=2; id | random_string ----+--------------- (0 rows) test=>
test=> explain (analyze,verbose) select id,random_string from data_tbl order by id desc limit 1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------ Foreign Scan on public.data_tbl (cost=1.00..2.00 rows=1 width=40) (actual time=7.654..7.665 rows=1 loops=1) Output: id, random_string Remote server startup cost: 25 Remote query: SELECT `id`, `random_string` FROM `test`.`data_tbl` ORDER BY `id` IS NOT NULL, `id` DESC LIMIT 1 Planning Time: 19.757 ms Execution Time: 10.775 ms (6rows) test=>

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70033867/viewspace-3108348/ ,如需转载,请注明出处,否则将追究法律责任。