一个商品表需要销量这个字段吗?
一个商品表需要销量这个字段吗?
根据表设计的范式 销量可以由订单记录得出(或者建一个商品每日效果表,记录每天的效率),但是这样在实际中却遇到了这样的问题?
怎么查询时怎么根据商品的销量排序呢,还有按点赞数排序呢,收藏数呢?想淘宝这样的怎么做呢?
回复内容:
一个商品表需要销量这个字段吗?
根据表设计的范式 销量可以由订单记录得出(或者建一个商品每日效果表,记录每天的效率),但是这样在实际中却遇到了这样的问题?
怎么查询时怎么根据商品的销量排序呢,还有按点赞数排序呢,收藏数呢?想淘宝这样的怎么做呢?
销量是必须的,但是这一列可以不是实时的,你应该记上统计出这个数据的时候的时间。
我的建议是,你先给订单时间加index,然后每过一小时(这个时间看你的售货速度)统计一下cache下当时的销量,然后把销量和时间这两个列存下来。你在前端显示真实的销量的时候,就可以把cache的数据,加上cache之后发生的订单的总和相加。这样你过一段时间就incrementally地做一下,问题就解决了。
订单时间加index的意思就是说,你知道你目前的cache是到譬如说半个小时前,那你这样就很容易query出半个小时内发生的订单,每一次处理的数据都会非常少。因此这个系统的负担不会随着你订单的增加而变慢,你也不需要因为每一次订单就频繁的更改“销量”这个列而产生性能问题。
如果一秒钟就有几个人挤进来,那可能会总是有一点误差,不过稍微做点变通就解决了。
对于如何给销量排序,我觉得需要在缓存这一层来解决,不需要在数据库维护这个index。如果按照这种方法维护销量的话,虽然直接按照这个列排序是不正确的,但是他“基本正确”。对于这样的属性的数据有特殊的排序方法。其中的一种方法是,你做一个qsort的变形,但是用户看到哪里你才排序到哪里。通常你在前端让用户按照销量排序的话,他只会看最前面的或者最后面的。平摊到每一次查阅,复杂度基本是log(n)的。
这种方法基本上可以用到你的数据规模明显比淘宝少的时候。你真有了淘宝那么大的数据,那所有的事情都得改成分布式的。最后要么上spark,要么上sqlazure,要么上scope,做起来就差很远了。
表的设计应该按照实际情况来,如果这个字段用得频繁,考虑到查询效率可以增加。
在你有排序的场景下,销量字段是必须的,并且由于是经常更新的字段,很有可能会按实际情况拆分到子表里,举个例子,goods存储商品信息,goods_counter一对一goods表,并存储各种统计信息,包括不限于销量,查看数,收藏数,点赞数,并根据需要建立索引
很多实际的一定规模(用户数或者数据量)的系统,未必还会使用MySQL做排序,会把数据放入搜索引擎,比如elasticsearch, solr, sphinx等等,然后利用搜索引擎做排序
都按销量排序了,那就加上呗。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
