简体   繁体   English

两部分 MySQL 问题:访问特定的 MySQL 行和列性能

[英]Two-part MySQL question: Accessing specific MySQL row, and column performance

I have a table with about 150 websites listed in it with the columns "site_name", "visible_name" (basically a formatted name), and "description."我有一张表,其中列出了大约 150 个网站,其中列有“site_name”、“visible_name”(基本上是一个格式化的名称)和“description”。 For a given page on my site, I want to pull site_name and visible_name for every site in the table, and I want to pull all three columns for the selected site, which comes from the $_GET array (a URL parameter).对于我网站上的给定页面,我想为表中的每个网站提取 site_name 和 visible_name,并且我想提取所选网站的所有三列,这些列来自 $_GET 数组(URL 参数)。

Right now I'm using 2 queries to do this, one that says "Get site_name and visible_name for all sites" and another that says "Get all 3 fields for one specific site."现在我正在使用 2 个查询来执行此操作,一个是“获取所有站点的站点名称和可见名称”,另一个是“获取一个特定站点的所有 3 个字段”。 I'm guess a better way to do it is:我猜一个更好的方法是:

SELECT * FROM site_list;

thus reducing to 1 query, and then doing the rest post-query, which brings up 2 questions:从而减少到 1 个查询,然后执行 rest 后查询,这会带来 2 个问题:

  1. The "description" field for each site is about 200-300 characters.每个站点的“描述”字段约为 200-300 个字符。 Is it bad from a performance standpoint to pull this for all 150 sites if I'm only using it for 1 site?如果我只将它用于 1 个站点,那么从性能的角度来看,将其用于所有 150 个站点是否很糟糕?
  2. How do I reference the specific row from the MySQL result set for the site specificed in the URL?如何从 MySQL 结果集中为 URL 中指定的站点引用特定行? For example, if the URL is "mysite.com/results?site_name=foo" how do I do the post-query equivalent of SELECT * FROM site_list where site_name=foo;例如,如果 URL 是“mysite.com/results?site_name=foo”,我该怎么做SELECT * FROM site_list where site_name=foo; ? ?

I don't know how to get the data for "site_name=foo" without looping through the entire result array and checking to see if site_name matches the URL parameter.我不知道如何在不遍历整个结果数组并检查 site_name 是否与 URL 参数匹配的情况下获取“site_name = foo”的数据。 Isn't there a more efficient way to do it?没有更有效的方法吗?

Thanks,谢谢,

Chris克里斯

PS: I noticed a similar question on stackoverflow and read through all the answers but it didn't help in my situation, which is why I'm posting this. PS:我在stackoverflow上注意到了一个类似的问题,并阅读了所有答案,但这对我的情况没有帮助,这就是我发布这个的原因。

Thanks,谢谢,

Chris克里斯

Generally, it's good practice to have an 'id' field in the table as an auto_increment value.通常,最好在表中使用“id”字段作为 auto_increment 值。 Then, you would:然后,你会:

SELECT id,url,display_name FROM table;

and you'd have the 'id' value there to later:你以后会有'id'值:

SELECT * FROM table WHERE id=123;

That's probably your most efficient method if you had WAAAY more entries in the table.如果表中有 WAAAY 更多条目,这可能是您最有效的方法。

However, with only 150 rows in the table, you're probably just fine doing但是,表中只有 150 行,您可能做得很好

SELECT * FROM table;

and only accessing that last field for a matching row based on your criteria.并且仅根据您的条件访问匹配行的最后一个字段。

If you only need the description for the site named foo you could just query the database with SELECT * FROM site_list WHERE site_name = 'foo' LIMIT 1如果您只需要名为 foo 的站点的描述,您可以使用 SELECT * FROM site_list WHERE site_name = 'foo' LIMIT 1 查询数据库

Otherwise you would have to loop though the result array and do a string comparison on site_name to find the correct description.否则,您将不得不遍历结果数组并在 site_name 上进行字符串比较以找到正确的描述。

I believe what you do now, keeping sperated queries for getting a list of sites with just titles and one detailed view with description for a single given site, is good.我相信您现在所做的,保持分散查询以获取仅包含标题的站点列表和带有单个给定站点描述的详细视图,这很好。 You don't pull any unneeded data and both queries being very simple are fast.您不会提取任何不需要的数据,而且两个非常简单的查询都很快。

It is possible to combine both your queries into one, using left join, something maybe like:可以使用左连接将两个查询合并为一个,可能类似于:

SELECT s1.site_name, s1.visible_name, s2.description
FROM site_list s1
LEFT JOIN
  ( SELECT site_name, description
  FROM site_list
  WHERE site_name = 'this site should go with description' ) s2
ON s2.site_name = s1.site_name

resulting in all sites without matching name having NULL as description, you could even sort it using导致所有没有匹配名称的站点都具有 NULL 作为描述,您甚至可以使用对其进行排序

ORDER BY description DESC, site_name

to get the site with description as first fetched row, thus eliminating need to iterate through results to find it, but mysql would have to do a lot more work to give you this result, negating any possible gain you could hope for.获取带有描述的站点作为第一个获取的行,从而无需遍历结果来找到它,但是 mysql 必须做更多的工作才能给你这个结果,否定你可能希望的任何可能的收益。 So basically stick to what you have now, its good.所以基本上坚持你现在拥有的,它很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM