• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    KeepingyourdataworkontheserverusingUNION_MySQL

    來源:懂視網 責編:小采 時間:2020-11-09 20:03:31
    文檔

    KeepingyourdataworkontheserverusingUNION_MySQL

    KeepingyourdataworkontheserverusingUNION_MySQL:I have found myself using UNION in MySQL more and more lately. In this example, I am using it to speed up queries that are using IN clauses. MySQL handles the IN clause like a big OR operation. Recently, I created what looks like a very cra
    推薦度:
    導讀KeepingyourdataworkontheserverusingUNION_MySQL:I have found myself using UNION in MySQL more and more lately. In this example, I am using it to speed up queries that are using IN clauses. MySQL handles the IN clause like a big OR operation. Recently, I created what looks like a very cra
    I have found myself using UNION in MySQL more and more lately. In this example, I am using it to speed up queries that are using IN clauses. MySQL handles the IN clause like a big OR operation. Recently, I created what looks like a very crazy query using UNION, that in fact helped our MySQL servers perform much better.

    With any technology you use, you have to ask yourself, "What is this tech good at doing?" For me, MySQL has always been excelent at running lots of small queries that use primary, unique, or well defined covering indexes. I guess most databases are good at that. Perhaps that is the bare minimum for any database. MySQL seems to excel at doing this however. We had a query that looked like this:

    select category_id, count(*) from some_table
    where
    article_id in (1,2,3,4,5,6,7,8,9) and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id

    There were more things in the where clause. I am not including them all in these examples. MySQL does not have a lot it can do with that query. Maybe there is a key on the date field it can use. And if the date field limits the possible rows, a scan of those rows will be quick. That was not the case here. We were asking for a lot of data to be scanned. Depending on how many items were in the in clauses, this query could take as much as 800 milliseconds to return. Our goal at DealNews is to have all pages generate in under 300 milliseconds. So, this one query was 2.5x our total page time.

    In case you were wondering what this query is used for, it is used to calculate the counts of items in sub categories on our category navigation pages. On this page it's the box on the left hand side labeled "Category". Those numbers next to each category are what we are asking this query to return to us.

    Because I know how my data is stored and structured, I can fix this slow query. I happen to know that there are many fewer rows for each item for article_id than there is for category_id. There is also a key on this table on article_id and some_date_time. That means, for a single article_id, MySQL could find the rows it wants very quickly. Without using a union, the only solution would be to query all this data in a loop in code and get all the results back and reassemble them in code. That is a lot of wasted round trip work for the application however. You see this pattern a fair amount in PHP code. It is one of my pet peeves. I have written before about keeping the data on the server . The same idea applies here. I turned the above query into this:

    select category_id, sum(count) as count from 
    (
    (
    select category_id, count(*) as count from some_table
    where
    article_id=1 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=2 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=3 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=4 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=5 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=6 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=7 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=8 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    union all
    (
    select category_id, count(*) as count from some_table
    where
    article_id=9 and
    category_id in (11,22,33,44,55,66,77,88,99) and
    some_date_time > now() - interval 30 day
    group by
    category_id
    )
    ) derived_table
    group by
    category_id
    Pretty gnarly looking huh? The run time of that query is 8ms. Yes, MySQL has to perform 9 subqueries and then the outer query. And because it can use good keys for the subqueries, the total execution time for this query is only 8ms. The data comes back from the database ready to use in one trip to the server. The page generation time for those pages went from a mean of 213ms with a standard deviation of 136ms to a mean of 196ms and standard deviation of 81ms. That may not sound like a lot. Take a look at how much less work the MySQL servers are doing now.mysql graph showing decrease in rows read

    The arrow in the image is when I rolled the change out. Several other graphs show the change in server performance as well.

    The UNION is a great way to keep your data on the server until it's ready to come back to your application. Do you think it can be of use to you in your application?

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    KeepingyourdataworkontheserverusingUNION_MySQL

    KeepingyourdataworkontheserverusingUNION_MySQL:I have found myself using UNION in MySQL more and more lately. In this example, I am using it to speed up queries that are using IN clauses. MySQL handles the IN clause like a big OR operation. Recently, I created what looks like a very cra
    推薦度:
    標簽: the mysql on
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 精品精品国产自在久久高清| 国产成人无码精品久久久免费 | 久久精品蜜芽亚洲国产AV| 精品久久香蕉国产线看观看亚洲| 最新国产精品拍自在线播放| 国产日韩久久久精品影院首页| 国产精品久久一区二区三区| 色欲国产麻豆一精品一AV一免费 | 国产精品人人爽人人做我的可爱 | 青青草国产精品久久| 精品视频无码一区二区三区 | 亚洲国产精品成人| 国内精品免费久久影院| 亚洲精品视频在线| 国产精品久久免费| 国产精品素人搭讪在线播放 | 99精品久久精品一区二区| 日韩精品无码专区免费播放| 免费精品国产自产拍在线观看| 国产精品无码久久久久| 国产VA免费精品高清在线| 欧美日韩在线亚洲国产精品| 国产亚洲美女精品久久久久狼| 99久久精品国产高清一区二区| 精品永久久福利一区二区 | 亚洲精品视频在线看| 久久精品国产只有精品66| 精品多毛少妇人妻AV免费久久| 国产精品成人69XXX免费视频| 高清日韩精品一区二区三区| 久久九九有精品国产23百花影院| 精品国产麻豆免费人成网站| 国产在线拍揄自揄视精品不卡| 久久这里只精品国产99热| 欧美精品天天操| 999国产精品色在线播放| 国产精品va久久久久久久| 国产三级精品三级| 久久久久九九精品影院| 久久精品国产一区二区三区不卡 | 奇米精品一区二区三区在线观看|