php - Sql高效快速搜索

标签 php mysql sql mysqli

我正在使用 php 和 sql 制作一个大型网站,该网站将包含许多用户,并且我需要尽快建立我的数据库。 这是从名为 upload 的数据库表中获取 posts 值并将其存储在数组中的代码

//connect to database
$a = 'localhost';
$b = 'root';
$c = '';
$d = 'petsnote';
$connect = mysqli_connect($a, $b, $c, $d);

//get posts images variables
$images = array();
$cat = array();
$id = array();

//get posts info
if($connect){
    $query = "SELECT * FROM `upload` WHERE `Active`='1'";
    if($query_run = mysqli_query($connect, $query)){
        while($get = mysqli_fetch_assoc($query_run)){
            array_push($images, $get['Image_Name']);
            array_push($cat, $get['PostCategory']);
            array_push($id, $get['Id']);
        }

        $loop = count($images);
    }
}

我从数据库中获取所有帖子信息并将它们存储在一个数组中,并使用该数组仅查看某些值。 这是一种高效、专业、快速的做事方式吗?

最佳答案

您的查询只需询问它将使用的内容:

SELECT Id, Image_Name, PostCategory FROM upload WHERE Active='1'

但这不会显着加快任何速度。

更重要的是数据库结构。从 MySQL 运行 SHOW CREATE TABLE upload 并确保您要搜索的每一列(例如本例中的 Active)都已建立索引。

不过,除非您要处理数万行,否则您不会看到太大的差异。

关于php - Sql高效快速搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34297044/

相关文章:

php - 当 2 个 MySQL 连接使用相同的临时表名时,它们是否可能崩溃?

php(ssh2,tunnel),我无法连接我的远程mysql服务器

java - 部署 Web 应用程序后出现“连接过多”错误

mysql - MySQL 和 MariaDB 主键默认顺序的区别?

sql - 计算每个类别中的项目

java - 这个实体模型的HQL?

php - 在所有输入都填满之前,如何停止重新加载页面?

java - 为什么 SQL 索引会转到错误的列?

php - Wordpress DB select 始终返回空

php - 您可以访问 iframe 加载外部网站的内容吗?