English 中文(简体)
• 铁路长
原标题:Paginating long text in Rails w/ MySQL

设想长篇案文的现行最佳做法是什么,例如将长篇文章分成多页?

我可以考虑的两种选择:

  • store the string in a longtext column, slice it to an array and paginate over that. However, I m worried about memory usage and doing too much processing on the page load.
  • slice up the text and store it in a page model (Article has_many :pages). Should work fine, but we d lose the flexibility of changing the page length.

什么是最佳办法? 是否有人失踪?

问题回答

我的建议是采用第一种办法,将切合结果放在一个十字架上(如上线),以便你保持改变页数的灵活性,而不必在你服务时将页数隔开。

如果案文通常少于10MB,将案文分成行动是完全合理的。 铁路的记忆管理比这要大得多,因此,你不要这样做。

然而,如果案文通常超过10元,则你应从贵数据库中直接选择。 例如,如果你重新使用MySQL和“积极记录”,你可以这样做:

# assuming fields `id`, and `body`
class Article < ActiveRecord
  def self.find_page(id, page_number, page_size = 10_000)
    page_number = page_number.to_i
    page_size = page_size.to_i
    pos = page_size * (page_number - 1)
    # http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring
    find(id, :select => "id, SUBSTRING(body, #{pos}, #{page_size}) AS body")
  end
end

(10MB当然是任意的编号。) 第1类





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签