1

这是背景,我有一个 API 方法,它将一个标准化的名称列表发送到我的服务器,然后将名称与给定的 Persons 对象匹配。每个人都有许多我想返回的与他们相关的 Post 对象。我的数据库查询如下所示:

 @articles = Article.joins(:artists).select("artists.*").where({:artists=>{:name=>keys}}).group("#{Article.col_list}").order("articles.publish_date DESC").limit(25)

当名称列表很大(超过 100 个)时,此查询在我的生产服务器上可能需要长达 2 秒的时间。如何快速优化此查询?

这是查询的解释。

 Limit  (cost=9144.19..9144.20 rows=25 width=802) (actual time=4341.409..4341.431 rows=25 loops=1)
   ->  Sort  (cost=9144.19..9150.74 rows=13096 width=802) (actual time=4341.408..4341.416 rows=25 loops=1)
         Sort Key: articles.publish_date
         Sort Method:  top-N heapsort  Memory: 47kB
         ->  HashAggregate  (cost=9030.99..9070.28 rows=13096 width=802) (actual time=4255.854..4315.125 rows=3916 loops=1)
               ->  Nested Loop  (cost=1581.27..8978.60 rows=13096 width=802) (actual time=7.443..3337.624 rows=39920 loops=1)
                     ->  Hash Join  (cost=1581.27..7650.04 rows=13096 width=4) (actual time=7.426..2627.426 rows=39920 loops=1)
                           Hash Cond: (articles_artists.artist_id = artists.id)
                           ->  Seq Scan on articles_artists  (cost=0.00..5476.38 rows=737461 width=8) (actual time=0.019..1310.806 rows=737515 loops=1)
                           ->  Hash  (cost=1579.83..1579.83 rows=413 width=4) (actual time=7.383..7.383 rows=300 loops=1)
                                 Buckets: 1024  Batches: 1  Memory Usage: 8kB
                                 ->  Bitmap Heap Scan on artists  (cost=477.60..1579.83 rows=413 width=4) (actual time=4.959..7.152 rows=300 loops=1)
                                       Recheck Cond: ((name)::text = ANY ('{"A BOY NAMED EARTH","A ROCKET TO MOON","A SKYLIT DRIVE","A VISIBLE BOY","AARON GILLESPIE",AB,"ABBIE BARRETT",ABERFELDY,"ABRA NIIRE","ABSOLUTE ZERO",ACDC,"ADAM GREEN",ADELE,ADO,"ADRINA THORPE",ADVENTURE,AEROSMITH,"AESOP ROCK",AFI,AIR,"AIR TRAFFIC",AKON,"AL GREEN","ALANIS MORISSETTE",ALCENDOR,"ALEX CHILTON","ALEXI MURDOCH","ALIAS EHREN","ALICE RUSSELL","ALICIA KEYS","AMANDA PALMER","AMERICAN FOOTBALL","AMY MACDONALD","AMY WINEHOUSE","ANALOG REBELLION",ANBERLIN,"ANDREW BIRD","ANDREW HILL","ANGELS AIRWAVES",AQUEDUCT,"ARCADE FIRE","ARETHA FRANKLIN","ART BRUT","ART GARFUNKEL","ARTIC MONKEYS","ARTIC MONKEYS","AS TALL AS LIONS",ASHANTI,"ATLAS SOUND","ATTACK ATTACK!",AUTOLUX,"AVENUE D","AVRIL LAVIGNE","B JU",BACK2SQUARE1,"BAD ENGLISH",BALMORHEA,"BAND OF HORSES",BARCELONA,"BAXTER DURY","BEASTIE BOYS",BEATLES,BECK,"BELLE AND SEBASTIAN","BEN FOLDS FIVE","BEN MARTIN","BETTE MIDLER","BETTIE SERVEERT",BEYONCE,"BIFFY CLYRO","BILLY BRAGG","BILLY BRAGG AND BLOKES","BILLY JOEL","BILLY OCEAN",BIRDY,"BLACK EYED PEAS","BLACK KIDS",BLACKSTONE,"BLIND PILOT","BLINK 182","BLOC PARTY","BLOODHOUND GANG",BLUEBOY,BLUR,"BOB DYLAN","BOB MARLEY","BOB MARLEY WAILERS","BOMBAY BICYCLE CLUB","BON IVER","BONNIE TYLER",BONOBO,"BOX CAR RACER","BOYS LIKE GIRLS","BOYZ II MEN",BREAKBOT,"BRENDAN BENSON","BRETT ANDERSON",BRICOLAGE,"BRITNEY SPEARS",BRONCO,"BRUCE SPRINGSTEEN","BRUNO MARS",BUSH,"BUSTA RHYMES",BUSTED,CAESARS,"CAGE ELEPHANT",CAKE,CALLA,"CALVIN HARRIS",CAMRON,CAMRON,CAMP,CAMPING,"CANDIE PAYNE",CASHEW,"CASSANDRA WILSOM",CASSIDY,"CATE LE BON","CHARLIE FEATHERS","CHERRY GHOST",CHIC,CHICAGO,CHINGY,"CHRIS BROWN","CHRISTINA AGUILERA","CHRISTINA STURMER",CHUMBAWAMBA,CIRCLE,"CLARENCE CARTER",CLIPSE,"COBRA STARSHIP",COLDPLAY,COMMON,CONSPIRATORS,COOLIO,COPELAND,"COPY HAHO",CORNELIUS,CREED,CULTS,"CYNDI LAUPER","CYPRESS HILL","CAT EMPIRE",CURE,"DA LATA","DAFT PUNK",DALMINJO,DAMERO,"DARREN HANLON","DASHBOARD CONFESSIONAL",DATAROCK,"DAVID BOWIE","DAVID E SUGAR","DAVID GUETTA","DAVID KITT","DEATH CAB FOR CUTIE","DIANA KRALL",DIGITALISM,"DINOSAUR JR","DISCO INFERNO",DISCOVERY,"DIZZEE RASCAL",DMX,"DON BLACKMAN","DR DOG",DRAKE,DRUGSTORE,DIPLOMATS,DREAM,EAGLES,EDITORS,ELBOW,ELECTRELANE,"ELIZA DOOLITTLE","ELLE MILANO","ELLIE GOULDING","ELTON JOHN","ELVIS COSTELLO WITH BURT BACHARACH","ELVIS PRESLEY",EMBRACE,EMINEM,"EMMY GREAT","EMPIRE! EMPIRE!","ERIC CHURCH","ERIC CLAPTON","ERIN MCCARLEY",ERRORS,"EUGENE KELLY","EXPLOSIONS IN SKY",FABOLOUS,FAILURE,"FAITH EVANS","FAR EAST MOVEMENT",FAUNTS,FEIST,FELT,FIELDS,"FLEET FOXES","FLIGHT OF CONCHORDS","FLORENCE MACHINE",FOALS,"FOO FIGHTERS","FOSTER PEOPLE","FOUR TET","FRANK TURNER","FRANZ FEDINAND",FREE,"FLAMING LIPS",GORILLAZ,"GRATEFUL DEAD",IDLEWILD,"IMANI COPPOLA","IMOGEN HEAP",INCUBUS,INCUBUS,"IRON WINE","J DILLA","J COLE",JACK,"JACK JOHNSON",JACOBITES,"JAMES BLAKE","JAMES BLUNT","JAMES BONG","JAMES BROWN","JAMIE T","JAN DELAY","JANE CULLUM","JASON DERULO","JASON MORAN","JAY Z","JAY Z","JEFF WAYNE","JENNIFER LOPEZ","JENNY LEWIS",JEREMIH,"JIM JONES","JIMMY EAT WORLD","JOHN COLTRANE","JOHN LEGEND","JOHN MAYER","JOY DIVISION","JUELZ SANTANA","JUNIOR BOYS","JUSTIN TIMBERLAKE","KAISER CHEIFS",KANTE,"KANYE WEST",KARMA,"KATE NASH","KATE NASH","KATY PERRY",KESHA,KEANE,KEANE,"KELLY ROWLAND","KENNY G","KERI HILSON","KEVIN AYERS","KID CUDI","KID HARPOON","KID LOCO","KID ROCK","KINGS OF LEON",KILLERS,KOOKS,"LADY GAGA","LADY SOVEREIGN","LED ZEPPELIN","LEE DORSEY",LEMAR,LEMONGRASS,"LEROY HUTSON","LIL WAYNE","LILY ALLEN","LITTLE COMETS","LITTLE JOY","LL COOL J",LLOYD,"LLOYD BANKS","LONDON LOUNGE",LOST,LOVE,LUDACRIS,LUDIQUE,"LUPE FIASCO","LUTHER VANDROS","MACHINE HEAD","MACY GRAY",MADONNA,MAINO,"MANU CHAO","MAREN MONTAUK","MARIAH CAREY","MARK FRY","MARK MORRISON","MARK RONSON","MAROON 5","MARY J BLIGE","MASSIVE ATTACK","MAT KEARNEY","MATTHEW DEAR","MEAT LOAF",METALLICA,MGMT,"MICHAEL GIACCHINO","MICHAEL JACKSON","MILES DAVIS","MISSY ELLIOTT","MOBB DEEP","MODEST MOUSE","MODEST MOUSE",MONK,MOON,MORRISSEY,MOTZ,"MOVING HEARTS",MUSE,MUTEMATH,"MY WRITES","MORNING OF",NAS,"NE YO","NICKI MINAJ","NINE INCH NAILS","P DIDDY","PATRICK WATSON","PAUL MCCARTNEY","PEARL JAM","PEPE ALGILAR","PEPE WHITE","PETER GABRIEL",PHARRELL,"PHIL COLLINS",PHISH,PITBULL,PIXIES,POLICE,PRINCE,"R KELLY",RADIOHEAD,RAKIM,RANK,"RASCAL FLATTS","RAY CHARLES","RED HOT CHILI PEPPERS",REDNEX,REENO,"RELIENT K","RICK ROSS","RICKY NELSON",RIFT,RIHANNA,"ROBBIE WILLIAMS","ROD STEWART","RODNEY HUNTER",ROKOKO,"ROLLING STONES","ROSCOE DASH","ROY DAVIS JR","RYAN LESLIE","RYAN LESLIE",RZA,"SARAH MCLACHLAN","SARAH RUSSELL",SEAL,"SHANIA TWAIN",SHERWOOD,"SMASH MOUTH",SMITHS,"SNOOP DOGG","SOMETHING CORPORATE",SPOON,SPOON,"ST LUNATICS",STARS,"STAT QUO","STEELY DAN","STEVIE WONDER","STYLES P","SUNSET RUBDOWN",SUPERCHUNK,"SWIZZ BEATZ","SECRET HANDSHAKE",SMITHS,"T PAIN",TI,"TALIB KWELI","TAYLOR SWIFT","THROWING MUSES",TLC,"TOM JONES","TOM PETTY","TONY YAYO",TOOL,"TORI AMOS",TORTOISE,TRAIN,"TREY SONGZ","TRIBE CALLED QUEST",TUBBS,TYCHO,"WHITE STRIPES","WAKA FLOCKA FLAME",WALE,WILCO,"WILL SMITH",U2,USHER,"YEAH YEAH YEAHS","YEAR OF RABBIT","YOUNG BUCK","YOUNG JEEZY","ZAC BROWN BAND",2PAC,"50 CENT"}'::text[]))
                                       ->  Bitmap Index Scan on index_artists_on_name  (cost=0.00..477.58 rows=413 width=0) (actual time=4.897..4.897 rows=311 loops=1)
                                             Index Cond: ((name)::text = ANY ('{"A BOY NAMED EARTH","A ROCKET TO MOON","A SKYLIT DRIVE","A VISIBLE BOY","AARON GILLESPIE",AB,"ABBIE BARRETT",ABERFELDY,"ABRA NIIRE","ABSOLUTE ZERO",ACDC,"ADAM GREEN",ADELE,ADO,"ADRINA THORPE",ADVENTURE,AEROSMITH,"AESOP ROCK",AFI,AIR,"AIR TRAFFIC",AKON,"AL GREEN","ALANIS MORISSETTE",ALCENDOR,"ALEX CHILTON","ALEXI MURDOCH","ALIAS EHREN","ALICE RUSSELL","ALICIA KEYS","AMANDA PALMER","AMERICAN FOOTBALL","AMY MACDONALD","AMY WINEHOUSE","ANALOG REBELLION",ANBERLIN,"ANDREW BIRD","ANDREW HILL","ANGELS AIRWAVES",AQUEDUCT,"ARCADE FIRE","ARETHA FRANKLIN","ART BRUT","ART GARFUNKEL","ARTIC MONKEYS","ARTIC MONKEYS","AS TALL AS LIONS",ASHANTI,"ATLAS SOUND","ATTACK ATTACK!",AUTOLUX,"AVENUE D","AVRIL LAVIGNE","B JU",BACK2SQUARE1,"BAD ENGLISH",BALMORHEA,"BAND OF HORSES",BARCELONA,"BAXTER DURY","BEASTIE BOYS",BEATLES,BECK,"BELLE AND SEBASTIAN","BEN FOLDS FIVE","BEN MARTIN","BETTE MIDLER","BETTIE SERVEERT",BEYONCE,"BIFFY CLYRO","BILLY BRAGG","BILLY BRAGG AND BLOKES","BILLY JOEL","BILLY OCEAN",BIRDY,"BLACK EYED PEAS","BLACK KIDS",BLACKSTONE,"BLIND PILOT","BLINK 182","BLOC PARTY","BLOODHOUND GANG",BLUEBOY,BLUR,"BOB DYLAN","BOB MARLEY","BOB MARLEY WAILERS","BOMBAY BICYCLE CLUB","BON IVER","BONNIE TYLER",BONOBO,"BOX CAR RACER","BOYS LIKE GIRLS","BOYZ II MEN",BREAKBOT,"BRENDAN BENSON","BRETT ANDERSON",BRICOLAGE,"BRITNEY SPEARS",BRONCO,"BRUCE SPRINGSTEEN","BRUNO MARS",BUSH,"BUSTA RHYMES",BUSTED,CAESARS,"CAGE ELEPHANT",CAKE,CALLA,"CALVIN HARRIS",CAMRON,CAMRON,CAMP,CAMPING,"CANDIE PAYNE",CASHEW,"CASSANDRA WILSOM",CASSIDY,"CATE LE BON","CHARLIE FEATHERS","CHERRY GHOST",CHIC,CHICAGO,CHINGY,"CHRIS BROWN","CHRISTINA AGUILERA","CHRISTINA STURMER",CHUMBAWAMBA,CIRCLE,"CLARENCE CARTER",CLIPSE,"COBRA STARSHIP",COLDPLAY,COMMON,CONSPIRATORS,COOLIO,COPELAND,"COPY HAHO",CORNELIUS,CREED,CULTS,"CYNDI LAUPER","CYPRESS HILL","CAT EMPIRE",CURE,"DA LATA","DAFT PUNK",DALMINJO,DAMERO,"DARREN HANLON","DASHBOARD CONFESSIONAL",DATAROCK,"DAVID BOWIE","DAVID E SUGAR","DAVID GUETTA","DAVID KITT","DEATH CAB FOR CUTIE","DIANA KRALL",DIGITALISM,"DINOSAUR JR","DISCO INFERNO",DISCOVERY,"DIZZEE RASCAL",DMX,"DON BLACKMAN","DR DOG",DRAKE,DRUGSTORE,DIPLOMATS,DREAM,EAGLES,EDITORS,ELBOW,ELECTRELANE,"ELIZA DOOLITTLE","ELLE MILANO","ELLIE GOULDING","ELTON JOHN","ELVIS COSTELLO WITH BURT BACHARACH","ELVIS PRESLEY",EMBRACE,EMINEM,"EMMY GREAT","EMPIRE! EMPIRE!","ERIC CHURCH","ERIC CLAPTON","ERIN MCCARLEY",ERRORS,"EUGENE KELLY","EXPLOSIONS IN SKY",FABOLOUS,FAILURE,"FAITH EVANS","FAR EAST MOVEMENT",FAUNTS,FEIST,FELT,FIELDS,"FLEET FOXES","FLIGHT OF CONCHORDS","FLORENCE MACHINE",FOALS,"FOO FIGHTERS","FOSTER PEOPLE","FOUR TET","FRANK TURNER","FRANZ FEDINAND",FREE,"FLAMING LIPS",GORILLAZ,"GRATEFUL DEAD",IDLEWILD,"IMANI COPPOLA","IMOGEN HEAP",INCUBUS,INCUBUS,"IRON WINE","J DILLA","J COLE",JACK,"JACK JOHNSON",JACOBITES,"JAMES BLAKE","JAMES BLUNT","JAMES BONG","JAMES BROWN","JAMIE T","JAN DELAY","JANE CULLUM","JASON DERULO","JASON MORAN","JAY Z","JAY Z","JEFF WAYNE","JENNIFER LOPEZ","JENNY LEWIS",JEREMIH,"JIM JONES","JIMMY EAT WORLD","JOHN COLTRANE","JOHN LEGEND","JOHN MAYER","JOY DIVISION","JUELZ SANTANA","JUNIOR BOYS","JUSTIN TIMBERLAKE","KAISER CHEIFS",KANTE,"KANYE WEST",KARMA,"KATE NASH","KATE NASH","KATY PERRY",KESHA,KEANE,KEANE,"KELLY ROWLAND","KENNY G","KERI HILSON","KEVIN AYERS","KID CUDI","KID HARPOON","KID LOCO","KID ROCK","KINGS OF LEON",KILLERS,KOOKS,"LADY GAGA","LADY SOVEREIGN","LED ZEPPELIN","LEE DORSEY",LEMAR,LEMONGRASS,"LEROY HUTSON","LIL WAYNE","LILY ALLEN","LITTLE COMETS","LITTLE JOY","LL COOL J",LLOYD,"LLOYD BANKS","LONDON LOUNGE",LOST,LOVE,LUDACRIS,LUDIQUE,"LUPE FIASCO","LUTHER VANDROS","MACHINE HEAD","MACY GRAY",MADONNA,MAINO,"MANU CHAO","MAREN MONTAUK","MARIAH CAREY","MARK FRY","MARK MORRISON","MARK RONSON","MAROON 5","MARY J BLIGE","MASSIVE ATTACK","MAT KEARNEY","MATTHEW DEAR","MEAT LOAF",METALLICA,MGMT,"MICHAEL GIACCHINO","MICHAEL JACKSON","MILES DAVIS","MISSY ELLIOTT","MOBB DEEP","MODEST MOUSE","MODEST MOUSE",MONK,MOON,MORRISSEY,MOTZ,"MOVING HEARTS",MUSE,MUTEMATH,"MY WRITES","MORNING OF",NAS,"NE YO","NICKI MINAJ","NINE INCH NAILS","P DIDDY","PATRICK WATSON","PAUL MCCARTNEY","PEARL JAM","PEPE ALGILAR","PEPE WHITE","PETER GABRIEL",PHARRELL,"PHIL COLLINS",PHISH,PITBULL,PIXIES,POLICE,PRINCE,"R KELLY",RADIOHEAD,RAKIM,RANK,"RASCAL FLATTS","RAY CHARLES","RED HOT CHILI PEPPERS",REDNEX,REENO,"RELIENT K","RICK ROSS","RICKY NELSON",RIFT,RIHANNA,"ROBBIE WILLIAMS","ROD STEWART","RODNEY HUNTER",ROKOKO,"ROLLING STONES","ROSCOE DASH","ROY DAVIS JR","RYAN LESLIE","RYAN LESLIE",RZA,"SARAH MCLACHLAN","SARAH RUSSELL",SEAL,"SHANIA TWAIN",SHERWOOD,"SMASH MOUTH",SMITHS,"SNOOP DOGG","SOMETHING CORPORATE",SPOON,SPOON,"ST LUNATICS",STARS,"STAT QUO","STEELY DAN","STEVIE WONDER","STYLES P","SUNSET RUBDOWN",SUPERCHUNK,"SWIZZ BEATZ","SECRET HANDSHAKE",SMITHS,"T PAIN",TI,"TALIB KWELI","TAYLOR SWIFT","THROWING MUSES",TLC,"TOM JONES","TOM PETTY","TONY YAYO",TOOL,"TORI AMOS",TORTOISE,TRAIN,"TREY SONGZ","TRIBE CALLED QUEST",TUBBS,TYCHO,"WHITE STRIPES","WAKA FLOCKA FLAME",WALE,WILCO,"WILL SMITH",U2,USHER,"YEAH YEAH YEAHS","YEAR OF RABBIT","YOUNG BUCK","YOUNG JEEZY","ZAC BROWN BAND",2PAC,"50 CENT"}'::text[]))
                     ->  Index Scan using articles_pkey on articles  (cost=0.00..0.10 rows=1 width=802) (actual time=0.006..0.006 rows=1 loops=39920)
                           Index Cond: (articles.id = articles_artists.article_id)
 Total runtime: 4343.603 ms
(18 rows)
4

3 回答 3

1

这个脱颖而出:

-> 对articles_artists 的Seq Scan(成本=0.00..5476.38 行=737461 宽度=8)(实际时间=0.019..1310.806 行=737515 循环=1)

如果您EXPLAIN在查询上运行并在任何地方看到“Seq Scan”一词,这表明您需要检查您是否拥有所有正确的索引。检查articles_artists并确保您在 和 上都有索引article_idartist_id假设您是这样命名它们的。

另外,VACUUM ANALYZE在您的数据库上运行 a 并查看是否可以加快速度。Postgresql 的查询优化器依赖于 生成的统计信息VACUUM ANALYZE,因此如果表在没有运行的情况下发生了显着变化,查询性能可能会受到影响。

最后,考虑分解该查询并让 ruby​​ 收集数据。有时你最好不要试图让数据库足够聪明以正确执行复杂的查询,而是在客户端手动优化它。

于 2012-04-06T06:30:20.150 回答
0

我看到两个问题。首先是上面Michele提到的seq scan。第二个是嵌套循环连接,可能是由于此时发生的记录太多。

我会看看您可以根据搜索条件进一步推动查询,以便选择尽可能少的项目。

我的猜测是索引不会有很大的不同,但额外的搜索条件会,特别是如果那些可以命中索引。

于 2012-09-26T04:33:20.247 回答
0

无论如何,您似乎都将获取最新的 25 篇文章。所以我会使用一个子查询来选择这 25 篇最近的文章并将结果与​​艺术家表连接起来。

为什么这会更好?因为您的查询构建了一个临时结果,该结果将每位艺术家与他们的所有文章连接起来,然后再对结果进行排序并获取最近的 25 个。

不……我错了。我按名称看到艺术家的 where 条件,这意味着我的解决方案不起作用。

所以我回来看看能不能帮上忙。如果您查看您发布的解释数据中的成本,艺术家名称“过滤器”是您花费最多的。另请注意,艺术家姓名都出现在此处,TWICE 暗示您在嵌套循环内不止一次为此操作付费。

因此,如果可以,请构建一个临时表,该表是通过过滤器的艺术家表。然后使用该表以我之前描述的方式执行文章查找,您的查询会更快。

于 2012-04-05T19:44:35.930 回答