will_paginate 和 geokit 不当行为(双重查询)
我正在使用 will_paginate 对 geokit 搜索结果进行分页。该代码可以工作,但是,在查看日志时,使用以下 will_paginate 调用时,它会加倍 geokit 查询:
@posts = Post.paginate :page => params[:page], :per_page => 1,
:origin => @search, :within => @miles, :include => :user
这是原始的非分页调用,按预期工作(单个查询):
@posts = Post.find(:all, :origin => @search, :within => @miles, :include => :user)
以下是使用时的日志输出第一个 will_paginate 调用:
Processing PostsController#search (for 127.0.0.1 at 2010-06-03 22:10:29) [POST]
Parameters: {"commit"=>"Search", "action"=>"search", "authenticity_token"=>"K9Btfu6p7pz2mt+lWH0Fx0O7qj+0QY21JpfgyWT738I=", "controller"=>"posts", "location"=>"new york"}
Google geocoding. Address: new york. Result: <?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>new york</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>New York, NY, USA</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>New York</SubAdministrativeAreaName><Locality><LocalityName>New York</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.8494506" south="40.5788125" east="-73.7498541" west="-74.2620917" />
</ExtendedData>
<Point><coordinates>-74.0059729,40.7142691,0</coordinates></Point>
</Placemark>
</Response></kml>
Post Load (0.7ms) SELECT *,
(ACOS(least(1,COS(0.710598048337988)*COS(-1.2916478932467)*COS(RADIANS(posts.lat))*COS(RADIANS(posts.lng))+
COS(0.710598048337988)*SIN(-1.2916478932467)*COS(RADIANS(posts.lat))*SIN(RADIANS(posts.lng))+
SIN(0.710598048337988)*SIN(RADIANS(posts.lat))))*3963.19)
AS distance FROM `posts` WHERE (((posts.lat>40.352844467866 AND posts.lat<41.075693732134 AND posts.lng>-74.4827993840952 AND posts.lng<-73.5291464159048)) AND (
(ACOS(least(1,COS(0.710598048337988)*COS(-1.2916478932467)*COS(RADIANS(posts.lat))*COS(RADIANS(posts.lng))+
COS(0.710598048337988)*SIN(-1.2916478932467)*COS(RADIANS(posts.lat))*SIN(RADIANS(posts.lng))+
SIN(0.710598048337988)*SIN(RADIANS(posts.lat))))*3963.19)
<= 25)) LIMIT 0, 1
Post Columns (2.4ms) SHOW FIELDS FROM `posts`
User Columns (2.2ms) SHOW FIELDS FROM `users`
User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
Google geocoding. Address: new york. Result: <?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>new york</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>New York, NY, USA</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>New York</SubAdministrativeAreaName><Locality><LocalityName>New York</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.8494506" south="40.5788125" east="-73.7498541" west="-74.2620917" />
</ExtendedData>
<Point><coordinates>-74.0059729,40.7142691,0</coordinates></Point>
</Placemark>
</Response></kml>
SQL (0.4ms) SELECT count(*) AS count_all FROM `posts` WHERE (((posts.lat>40.352844467866 AND posts.lat<41.075693732134 AND posts.lng>-74.4827993840952 AND posts.lng<-73.5291464159048)) AND (
(ACOS(least(1,COS(0.710598048337988)*COS(-1.2916478932467)*COS(RADIANS(posts.lat))*COS(RADIANS(posts.lng))+
COS(0.710598048337988)*SIN(-1.2916478932467)*COS(RADIANS(posts.lat))*SIN(RADIANS(posts.lng))+
SIN(0.710598048337988)*SIN(RADIANS(posts.lat))))*3963.19)
<= 25))
Rendering template within layouts/application
如您所见,KML/XML 和 SQL 查询增加了一倍。知道发生了什么事以及如何解决它吗?谢谢!
-托尼
I'm using will_paginate to paginate my geokit search results. The code works, however, when looking at the log it does double the geokit query when using the following will_paginate call:
@posts = Post.paginate :page => params[:page], :per_page => 1,
:origin => @search, :within => @miles, :include => :user
This is the original non-paginated call which works as expected (a single query):
@posts = Post.find(:all, :origin => @search, :within => @miles, :include => :user)
The following is the log output when using the first will_paginate call:
Processing PostsController#search (for 127.0.0.1 at 2010-06-03 22:10:29) [POST]
Parameters: {"commit"=>"Search", "action"=>"search", "authenticity_token"=>"K9Btfu6p7pz2mt+lWH0Fx0O7qj+0QY21JpfgyWT738I=", "controller"=>"posts", "location"=>"new york"}
Google geocoding. Address: new york. Result: <?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>new york</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>New York, NY, USA</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>New York</SubAdministrativeAreaName><Locality><LocalityName>New York</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.8494506" south="40.5788125" east="-73.7498541" west="-74.2620917" />
</ExtendedData>
<Point><coordinates>-74.0059729,40.7142691,0</coordinates></Point>
</Placemark>
</Response></kml>
Post Load (0.7ms) SELECT *,
(ACOS(least(1,COS(0.710598048337988)*COS(-1.2916478932467)*COS(RADIANS(posts.lat))*COS(RADIANS(posts.lng))+
COS(0.710598048337988)*SIN(-1.2916478932467)*COS(RADIANS(posts.lat))*SIN(RADIANS(posts.lng))+
SIN(0.710598048337988)*SIN(RADIANS(posts.lat))))*3963.19)
AS distance FROM `posts` WHERE (((posts.lat>40.352844467866 AND posts.lat<41.075693732134 AND posts.lng>-74.4827993840952 AND posts.lng<-73.5291464159048)) AND (
(ACOS(least(1,COS(0.710598048337988)*COS(-1.2916478932467)*COS(RADIANS(posts.lat))*COS(RADIANS(posts.lng))+
COS(0.710598048337988)*SIN(-1.2916478932467)*COS(RADIANS(posts.lat))*SIN(RADIANS(posts.lng))+
SIN(0.710598048337988)*SIN(RADIANS(posts.lat))))*3963.19)
<= 25)) LIMIT 0, 1
Post Columns (2.4ms) SHOW FIELDS FROM `posts`
User Columns (2.2ms) SHOW FIELDS FROM `users`
User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 1)
Google geocoding. Address: new york. Result: <?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>new york</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>New York, NY, USA</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>New York</SubAdministrativeAreaName><Locality><LocalityName>New York</LocalityName></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.8494506" south="40.5788125" east="-73.7498541" west="-74.2620917" />
</ExtendedData>
<Point><coordinates>-74.0059729,40.7142691,0</coordinates></Point>
</Placemark>
</Response></kml>
SQL (0.4ms) SELECT count(*) AS count_all FROM `posts` WHERE (((posts.lat>40.352844467866 AND posts.lat<41.075693732134 AND posts.lng>-74.4827993840952 AND posts.lng<-73.5291464159048)) AND (
(ACOS(least(1,COS(0.710598048337988)*COS(-1.2916478932467)*COS(RADIANS(posts.lat))*COS(RADIANS(posts.lng))+
COS(0.710598048337988)*SIN(-1.2916478932467)*COS(RADIANS(posts.lat))*SIN(RADIANS(posts.lng))+
SIN(0.710598048337988)*SIN(RADIANS(posts.lat))))*3963.19)
<= 25))
Rendering template within layouts/application
As you can see the KML/XML and SQL queries are doubled. Any idea what's going on and how I can fix it? Thanks!
-Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上这是标准行为。 will_paginate首先统计记录数,然后检索20行,具体取决于可见页面和页面上的行数
(因此查询并不完全相同)。
需要计数来显示页数。
但是您的日志记录对我来说并不完全有意义,因为 will_paginate 查询会将数量限制为 20(或任何页面大小),而且显示的查询并不完全相同:例如,我看到不同的条件。
Actually this is standard behaviour. will_paginate first counts the number of records and then retrieves 20 rows, depending on the visible page and number of rows on the page
(so the queries are not completely identical).
The count is needed to display the number of pages.
But your logging does not completely make sense to me, as the will_paginate query would limit the number to 20 (or whatever page-size), also the shown queries are not entirely identical: i see different conditions for instance.