douhao8456 2016-03-31 21:46
浏览 21

管理一个表或多个表中的不同类别?

I am making an article website. I have a database called CATEGORIES with tables: Articles, Entertainment, Lifestyle. Articles contains all the entries of both Entertainment and Lifestyle. Entertainment contains all entries of Entertainment type and Lifestyle contains all entries of Lifestyle type. Each table has 7 columns: id, category (category of the article), link (link of the article), title (title of the article), image (image URL of the article), Counter (the number of views of the article), dateStamp (the date in which the article was written).

Would it be more efficient to get rid of the Lifestyle and Entertainment tables, and just rely on the Articles table? The reason why I separated them this way is because when the user is reading let's say an Entertainment article, a random list of Entertainment articles are shown. To show random articles I take the highest id in the Entertainment table, get a random number between 6 and the highest id (6 is the number of articles I want to display), and then let's say I get 8 off of the random generator, then articles 8 though 3 are displayed (DESC LIMIT 6). Here's the code:

<?php

$MAX_ID = $db->query("SELECT MAX(id) FROM Entertainment");
$MAX_ID = $MAX_ID->fetch_array();
$MAX_ID = $MAX_ID[0];

$RAND_NUM = mt_rand(6, $MAX_ID);

$resultSet = $db->query("SELECT * FROM Entertainment 
                             WHERE id <= $RAND_NUM ORDER BY id DESC LIMIT 6");

if ($resultSet->num_rows != 0) {
    $conditional = true;
    $conditional2 = true;
    echo "<div class='row'>";
    while ($rows = $resultSet->fetch_assoc()) {
        $image = $rows["image"];
        $title = $rows["title"];
        $link = $rows["link"];
        $count = number_format($rows["Counter"]);

        if ($conditional == true && $conditional2 == true) {
            // display articles
        } else {
            // display other articles
        }
    }
}

If I removed the Entertainment table, then this query would not work, and I don't know how else I would make it. The final question is would it be more efficient (performance wise) to separate the tables into different categories or combine the tables into one table?

  • 写回答

1条回答 默认 最新

  • drjmrg8766 2016-04-03 00:13
    关注

    Never make one table per widget.

    If two tables have virtually identical columns, then they probably should be combined into a single table. It may need an extra column to tell the things apart. And changes to the indexes.

    Normalize, but don't over-normalize. Do not, for example normalize 'continuous' values like DATETIME or FLOAT.

    Do have a PRIMARY KEY on every table. Consider using a "natural" PK, or use an AUTO_INCREMENT. (Sometimes one is the obvious choice; sometimes the other; and sometimes it is a coin-flip.)

    Your code snippet smells like you want some 'random' rows. My blog on such.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题