javascript - 通过使用某些值得的小表的 javascript 数组来支持它来降低查询的复杂性

标签 javascript mysql sql arrays join

我的网站在请求自定义结果时非常依赖数据库查询。这些查询为某些数据内部连接了大约 6 个不同的表,这些表中的数据长期保持静态。我一直在考虑制作一个 javascript 数组并使用方法 1 来查找数据,而不是删除一些内部联接,而不是全部,但足以确保我的数据库不会超限。我有一个示例查询。

//search function
function FindData(WantedID) {
    var result = Array.filter(function( obj ) {
        return obj.ID== WantedID;
    });
    return result[0];
};

//query
SELECT ItemInfo1.SimpleDescription AS Item1Simple,
   ItemInfo1.FullDescription AS Item1Description,
   ItemInfo1.Name AS Item1Name,
   ItemInfo1.GoldCost AS Item1GoldCost,
   ItemInfo2.SimpleDescription AS Item2Simple,
   ItemInfo2.FullDescription AS Item2Description,
   ItemInfo2.Name AS Item2Name,
   ItemInfo2.GoldCost AS Item2GoldCost,
   ItemInfo3.SimpleDescription AS Item3Simple,
   ItemInfo3.FullDescription AS Item3Description,
   ItemInfo3.Name AS Item3Name,
   ItemInfo3.GoldCost AS Item3GoldCost,
   ItemInfo4.SimpleDescription AS Item4Simple,
   ItemInfo4.FullDescription AS Item4Description,
   ItemInfo4.Name AS Item4Name,
   ItemInfo4.GoldCost AS Item4GoldCost,
   ItemInfo5.SimpleDescription AS Item5Simple,
   ItemInfo5.FullDescription AS Item5Description,
   ItemInfo5.Name AS Item5Name,
   ItemInfo5.GoldCost AS Item5GoldCost,
   ItemInfo6.SimpleDescription AS Item6Simple,
   ItemInfo6.FullDescription AS Item6Description,
   ItemInfo6.Name AS Item6Name,
   ItemInfo6.GoldCost AS Item6GoldCost,
   ItemInfo7.SimpleDescription AS Item7Simple,
   ItemInfo7.FullDescription AS Item7Description,
   ItemInfo7.Name AS Item7Name,
   ItemInfo7.GoldCost AS Item7GoldCost,
   Spell11.Description AS Spell1Description,
   Spell11.Name AS Spell1Name,
   Spell11.SpellKey AS Spell1Key,
   Spell11.Cooldown AS Spell1Cooldown,
   Spell12.Description AS Spell2Description,
   Spell12.Name AS Spell2Name,
   Spell12.SpellKey AS Spell2Key,
   Spell12.Cooldown AS Spell2Cooldown,
   masteries.MasteryID,
   masteries.MasteryName,
   masteries.MasteryDescription,
   matchhistory.*,
   championdb.ChampName,
   player.Alias,
   player.RoleSlug,
   player.Region,
   player.AltRegion,
   player.playerid,
   player.SummonerIcon,
   teams.Name,
   teams.ID AS TeamID,
   player.League,
   player.Division,
   player.Points,
   player.isFreshBlood,
   player.isHotStreak,
   player.isVeteran,
   player.Wins,
   player.Losses,
   player.FirstChamp,
   player.SecondChamp,
   player.ThirdChamp
FROM matchhistory
   INNER JOIN player ON matchhistory.SummonerID = player.playerid
   INNER JOIN teams ON player.Team = teams.ID
   INNER JOIN summonerspells AS Spell11
      ON matchhistory.Spell1 = Spell11.SpellID
   INNER JOIN summonerspells AS Spell12
      ON matchhistory.Spell2 = Spell12.SpellID
   INNER JOIN items AS ItemInfo1 ON matchhistory.Item0 = ItemInfo1.ItemID
   INNER JOIN items AS ItemInfo2 ON matchhistory.Item1 = ItemInfo2.ItemID
   INNER JOIN items AS ItemInfo3 ON matchhistory.Item2 = ItemInfo3.ItemID
   INNER JOIN items AS ItemInfo4 ON matchhistory.Item3 = ItemInfo4.ItemID
   INNER JOIN items AS ItemInfo5 ON matchhistory.Item4 = ItemInfo5.ItemID
   INNER JOIN items AS ItemInfo6 ON matchhistory.Item5 = ItemInfo6.ItemID
   INNER JOIN items AS ItemInfo7 ON matchhistory.Item6 = ItemInfo7.ItemID
   INNER JOIN masteries ON matchhistory.KeystoneID = masteries.MasteryID
   INNER JOIN championdb ON matchhistory.ChampID = championdb.ChampID
ORDER BY matchhistory.TimeStamp DESC LIMIT 10;

我希望减少项目内部联接,因为它们在主表中有 6 个项目,我每次都需要进行内部联接以获取每个项目的特定数据。总共有 200 个项目。我正在使用 mysql,我也在考虑用 WHERE ID IN 替换 inner join 以提高查询速度。我还可以缓存数组结果以使其更快,数组数据是通过一次性查询生成的。我的数据库服务器与我的 webapp 服务器是分开的

Image of my explain and other details

最佳答案

你犯了几个错误

1) 您将整个数据库表连接到 javascript 中,然后尝试查找特定的匹配项。 这在性能方面非常糟糕,尤其是当您希望您的表增长时。哪里有条款是有原因的。

2) 您的数据库表似乎正确编入索引。 根据你的解释,mysql 正在对玩家表进行全表扫描,而不是扫描比赛历史表,从其他表中添加多行,创建用于排序的临时表(可能是 eaven 磁盘表),对其进行排序(没有索引),然后获取你的 10数据行。

您需要在您应该匹配的 ID 列上的匹配表上添加索引,而不是添加时间戳列以启用按索引排序。这必须与包含您要搜索的特定 ID 的 where 子句结合使用。

但是....根据您提供的代码部分,我不确定您的问题出在数据库上,因为 mysql 的每个小实例都必须在几毫秒内处理这个问题。

关于javascript - 通过使用某些值得的小表的 javascript 数组来支持它来降低查询的复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42283247/

相关文章:

python - 容差地从数据库中获取每n小时的数据

javascript - 如何让 Mocha 加载定义全局 Hook 或实用程序的 helper.js 文件?

mysql - 输入多个外键时无法在 SQL 中创建表

javascript - 更改 anchor 元素内 div 的类名

mysql - Azure SQL,触发之前?

mysql - 如何对表中的所有列执行 REGEXP?

MySQL将一列的设置值更新为两列的总和

mysql - 如何在 SQL 中获取三个连接中的第二个的最后一条记录

javascript - 如何更改悬停以单击 jquery slider

javascript - 使用 http post 请求传递有关事件更改的参数