- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在更新现有的基于 Web 的库存系统,该系统从 MySQL 数据库中提取数据。存储数据的主要结构是一对多关系的“items”和“tags”(items可以有多个对应的tags)
现有的数据前端系统是一个 Backbone.js 应用程序,它在登录时提取整个数据存储并在内存中操作该数据,必要时通过 RESTful 接口(interface)提交回数据库。 (这不是我设计系统的方式,但它现在是 Backbone 和 Spine 应用程序中的常见模式,也是大多数教程和书籍如何教授这些框架的方式)。
为了服务于前端捕获整个数据集(此时大约有 1000 个项目和 10,000 个项目标签)执行的初始提取,后端对项目表执行 SELECT 查询,然后执行后续的 SELECT查询每个获取的项目的标签表。显然,性能很糟糕。我认为这可以通过 JOIN 进行改进,认为一个选择查询优于 1000。以下查询获取我需要的数据,但即使在我的本地开发服务器上执行也需要超过 15 秒。是什么赋予了?我们可以在不设置缓存键值存储等额外基础设施的情况下改进这个系统或查询吗?
SELECT items.*, itemtags.id as `tag_id`, itemtags.tag, itemtags.type
FROM items LEFT OUTER JOIN
itemtags
ON items.id = itemtags.item_id
ORDER BY items.id;
表结构如下:
CREATE TABLE `items` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`num` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`length_inches` int(10) unsigned DEFAULT NULL,
`length_feet` int(10) unsigned DEFAULT NULL,
`width_inches` int(10) unsigned DEFAULT NULL,
`width_feet` int(10) unsigned DEFAULT NULL,
`height_inches` int(10) unsigned DEFAULT NULL,
`height_feet` int(10) unsigned DEFAULT NULL,
`depth_inches` int(10) unsigned DEFAULT NULL,
`depth_feet` int(10) unsigned DEFAULT NULL,
`retail_price` int(10) unsigned DEFAULT NULL,
`discount` int(10) unsigned DEFAULT NULL,
`decorator_price` int(10) unsigned DEFAULT NULL,
`new_price` int(10) unsigned DEFAULT NULL,
`sold` int(10) unsigned NOT NULL,
`push_date` int(10) unsigned DEFAULT NULL,
`updated` int(10) unsigned NOT NULL,
`created` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1747 DEFAULT CHARSET=latin1;
CREATE TABLE `itemtags` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10) unsigned NOT NULL,
`tag` varchar(100) NOT NULL,
`type` varchar(100) NOT NULL,
`created` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=61474 DEFAULT CHARSET=latin1;
最佳答案
我想你可以用这个:
SELECT *, a.id as `tag_id`, a.tag, a.type
FROM items LEFT OUTER JOIN
(SELECT id, item_id, tag, type from itemtags ORDER BY 1,2,3) a
ON items.id = a.item_id
ORDER BY items.id;
我并没有真正改变太多,只是别名。 a 并不表示任何重要的事情。
我没有填写表格,但您的原始查询用了 4 毫秒,我用了 1 毫秒。
http://sqlfiddle.com/#!2/b9551/6
无论您的数据集中有什么,您的应用程序都可以提取整个数据存储。由于数据存储和数据集不是同义词。
您也没有任何索引
。您应该在 ID, ITEM_ID
上放置一个索引,以便优化表以更快地返回结果。我使用 order by
在我的子查询中创建了一个索引。希望这会有所帮助。
关于MySQL OUTER LEFT JOIN 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20570415/
class Outer { class Inner { } } public class Demo { public static void main(
不确定我是否明白这一点,但我从 pyLint 收到验证错误说: Redefining name 'a' from outer scope (line 443) (redefined-outer-nam
我是 SQL 新手,最近开始在我的代码中实现联接,我希望检索的数据可以使用以下 SQL 语句来完成。但是,如您所知,SQLite3 不支持 RIGHT OUTER 和 FULL OUTER JOIN。
怀疑但不确定:是 FROM 中表格的简单列表-clause 根据定义是完全连接? SELECT * FROM table1, table2 我们在参数不相等的情况下连接表的情况是否是完全外连接的实现?
我想知道 numpy 中是否有一个功能与 np.outer 类似,但使用二维数组和向量作为输入参数。结果应该是一个 3 维数组。 例如,如果我有一个矩阵 M 和一个向量 v,我想将原始矩阵 M 堆叠
以下代码在 MSVC 上编译: #include class Bob { int a; friend class Outer; }; class Outer {
Spark SQL 文档指定 join()支持以下连接类型: Must be one of: inner, cross, outer, full, full_outer, left, left_out
谁能帮我解决以下问题.. 这里我们有1.左外连接2.两个右外连接 如何结合左右 O.J 并创建与上面相同的效果, 我需要重写相同的查询,这是因为我正在将旧的 ms SQL2009 转换为 ms SQL
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
使用 ANSI 语法的 Informix 查询性能是否有差异: SELECT .. LEFT OUTER JOIN some_table ON () LEFT OUTER JOIN some_
根据clang , gcc 和 vs2013,函数 Outer::f 是 not 类 Outer::Inner 的 friend 。 struct Outer { void f() {}
换句话说,对于嵌套/多个 JOIN SQL 语句,是否应该始终首先使用 INNER JOIN(要么将其放在顶部行或通过使用括号首先 INNER JOIN 两个表)并确保它在任何 OUTER JOIN
我们有以下查询。使用 LEFT OUTER 连接需要 9 秒才能执行。将 LEFT OUTER 更改为 LEFT INNER 可将执行时间减少到 2 秒,并且返回相同行数。由于无论连接类型如何,都正在
为什么在实例化通用外部类(连同内部类)时使用菱形运算符会在代码段 2 中产生错误,而代码段 1 却完全没问题? 我知道稀有类型是被禁止的,但我的情况不是稀有类型——在稀有类型中,外部和内部都是通用的,
我有 4 个表,如下所示。 表:类 ClassID | ClassSTD -------------------------------- 1 | STD-1 2
是否有关于“外部 =>”功能的文档?它看起来像一个带有推断类型的自类型注释。然而我有一种感觉,我错了。 如果是这样,是否只是表达对 super 的访问的不同方式? ? trait A extends
我有 2 个表 users 和 orders,我想获取 users 和他的订单数 SELECT `users`.*, `orders`.*,count(*) FROM `users` LEFT JOI
我有一个非常古老的 informix 过程,我想在我的新 PostgreSQL 数据库中使用它。我是 Postgres 的新手,我觉得连接很不一样。 这是我的旧 Informix 代码的一部分: CR
我非常擅长 SQL 语句,希望能得到一点帮助。我发现我需要对其中一些表使用 OUTER 联接,但 MySQL 没有该功能。 SELECT validclick.Campaign
对,这有点令人困惑,我不确定通过 sql 是否可行。 我的第一个表叫做成员,我目前有这个 user:ID type: firstname: lastname: 这是表中的数据 members tabl
我是一名优秀的程序员,十分优秀!