l********r 发帖数: 140 | 1 一个产品网站,假设所有产品有一个顺序。
随机到一个产品页面, 要求同时显示它的前K个和后K个产品。
要求效率高的。
有没有好办法?
我的笨办法是:
1) assume we get the current page's product.
2) query all products (per the question, they will be sorted in order)
3) find where is the current product in this sorted list. Then it is easy to
take the K ones before and after it.
4) of course some edge conditions will be: k is too big, or current product
is too close to the start / end already (or it is first / last one already).
PS: assume you can use a general backend database (like, MS SQL, Oracle,
MySQL) to save all the product information. |
|