์นดํ…Œ๊ณ ๋ฆฌ ๋ณด๊ด€๋ฌผ: wordpress

wordpress

์ฟผ๋ฆฌ์— ์‚ฌ์šฉ์ž ์ •์˜ ํ…Œ์ด๋ธ”์„ ํฌํ•จํ•˜๋„๋ก WP_Query๋ฅผ ํ™•์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๊ฐ€์ง€ ๋ฉ‹์ง„ ๊ถŒ์žฅ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” ์ง€๊ธˆ์ด ๋ฌธ์ œ์— ๋ฉฐ์น ์ด ์ง€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์‚ฌ์šฉ์ž ํŒ”๋กœ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— WordPress Answers์—์„œ ๋ช‡ ๊ฐ€์ง€ ๋ฉ‹์ง„ ๊ถŒ์žฅ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ถŒ์žฅ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒˆ ํ…Œ์ด๋ธ”์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

id  leader_id   follower_id
1   2           4
2   3           10
3   2           10

์œ„์˜ ํ‘œ์—์„œ ์ฒซ ๋ฒˆ์งธ ํ–‰์—๋Š” ID๊ฐ€ 2 ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ์žˆ๊ณ  ๊ทธ ๋’ค์— ID๊ฐ€ 4 ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ–‰์—์„œ ID๊ฐ€ 3 ์ธ ์‚ฌ์šฉ์ž ๋’ค์— ID๊ฐ€์žˆ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค ์„ธ ๋ฒˆ์งธ ํ–‰์—๋„ ๋™์ผํ•œ ๋…ผ๋ฆฌ๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ด์ œ ๋ณธ์งˆ์ ์œผ๋กœ WP_Query๋ฅผ ํ™•์žฅํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ๋ฆฌ๋” ๋งŒ ๊ฐ€์ ธ์˜จ ๊ฒŒ์‹œ๋ฌผ์„ ์ œํ•œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„ ํ‘œ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์‚ฌ์šฉ์ž ID 10์„ WP_Query์— ์ „๋‹ฌํ•˜๋ฉด ๊ฒฐ๊ณผ์—๋Š” ์‚ฌ์šฉ์ž ID 2 ๋ฐ ์‚ฌ์šฉ์ž ID 3์˜ ๊ฒŒ์‹œ๋ฌผ ๋งŒ ํฌํ•จ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๋ณ€์„ ์ฐพ์œผ๋ ค๊ณ  ๋งŽ์ด ๊ฒ€์ƒ‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ WP_Query ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด๋˜๋Š” ์ž์Šต์„œ๋ฅผ ๋ณด์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ์งˆ๋ฌธ์— ๋Œ€ํ•œ Mike Schinkel์˜ ๋‹ต๋ณ€ (ํ™•์žฅ WP_Query)์„ ๋ณด์•˜์ง€๋งŒ ๋‚ด ์š”๊ตฌ์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‹ค์ œ๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€ ๋‚˜๋ฅผ ๋„์™€ ์ค„ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข‹์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์š”์ฒญ์— ๋”ฐ๋ผ Mike์˜ ๋‹ต๋ณ€์— ๋Œ€ํ•œ ๋งํฌ :
๋งํฌ 1 , ๋งํฌ 2



๋‹ต๋ณ€

์ค‘์š” ๊ณ ์ง€ ์‚ฌํ•ญ :์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์€ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ wp_usermeta๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฒŒ์‹œ๋ฌผ์„ ์ฟผ๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž ์ง€์ • SQL์„ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค (๋‹จ, ๊ด€๋ฆฌ์ž ์„น์…˜์—์„œ ํŠน์ • ๊ด€๋ฆฌ์ž์—๊ฒŒ๋ณด๊ณ ํ•˜๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ์˜ ๋ชฉ๋ก์„ ๊ฐ€์ ธ ์˜ค๋ ค๋ฉด ์ผ๋ถ€ ์‚ฌ์šฉ์ž ์ง€์ • SQL์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค). ๊ทธ๋Ÿฌ๋‚˜ OP๊ฐ€ ์‚ฌ์šฉ์ž ์ง€์ • SQL ์ž‘์„ฑ์— ๋Œ€ํ•ด ์š”์ฒญ ํ–ˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ์€ ๊ธฐ์กด WordPress ์ฟผ๋ฆฌ์— ์‚ฌ์šฉ์ž ์ง€์ • SQL์„ ์‚ฝ์ž…ํ•˜๋Š” ํ˜„์žฌ ๋ชจ๋ฒ” ์‚ฌ๋ก€์ž…๋‹ˆ๋‹ค.

๋ณต์žกํ•œ ์กฐ์ธ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” join_where ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ์˜ ์กฐ์ธ, ์„ ํƒ ๋ฐ ๊ทธ๋ฃน ๋ณ„ ๋˜๋Š” ์ˆœ์„œ๋ณ„๋กœ ์„น์…˜์„ ์ˆ˜์ •ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ โ€˜posts_clausesโ€™ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋งค์šฐ ์œ ์šฉํ•œ ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค (๋‚จ์šฉํ•ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค!). WordPress ์ฝ”์–ด ๋‚ด์˜ ๋งŽ์€ ์ฝ”๋“œ ์ค„์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” SQL์˜ ๋‹ค์–‘ํ•œ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ / ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„ํ„ฐ ์ฝœ๋ฐฑ ์„œ๋ช…์€ ๋‹ค์Œ function posts_clauses_filter_cb( $clauses, $query_object ){ }๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค
$clauses.

์กฐํ•ญ

$clauses๋‹ค์Œ ํ‚ค๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ๊ฐ ํ‚ค๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ „์†ก ๋œ ์ตœ์ข… SQL ๋ฌธ์—์„œ ์ง์ ‘ ์‚ฌ์šฉ๋˜๋Š” SQL ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

  • ์–ด๋””์—
  • ๊ทธ๋ฃน ๋ณ„
  • ๋ถ™๋‹ค
  • ์ฃผ๋ฌธ
  • ๋šœ๋ ทํ•œ
  • ์ „์ง€
  • ์ œํ•œ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ…Œ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ (post_meta, user_meta ๋˜๋Š” taxonomies๋ฅผ ์ ˆ๋Œ€ ํ™œ์šฉํ•  ์ˆ˜์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰)์ด ์ ˆ ์ค‘ ํ•˜๋‚˜ ์ด์ƒ์„ ํ„ฐ์น˜ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค fields( ์˜ˆ : โ€œSELECTโ€ SQL ๋ฌธ์˜ ์ผ๋ถ€), join( โ€œFROMโ€์ ˆ์—์žˆ๋Š” ํ…Œ์ด๋ธ” ์ด์™ธ์˜ ๋ชจ๋“  ํ…Œ์ด๋ธ”)orderby .

์กฐํ•ญ ์ˆ˜์ •

์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ• $clauses์€ ํ•„ํ„ฐ์—์„œ ์–ป์€ ๋ฐฐ์—ด ์—์„œ ๊ด€๋ จ ํ‚ค๋ฅผ ํ•˜์œ„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค .

$join = &$clauses['join'];

์ด์ œ๋ฅผ ์ˆ˜์ • $joinํ•˜๋ฉด ์‹ค์ œ๋กœ ์ง์ ‘ ์ˆ˜์ • $clauses['join']๋˜๋ฏ€๋กœ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด$clauses ์„ ๋ฐ˜ํ™˜ ํ•  ๋•Œ .

์›๋ณธ ์ ˆ ๋ณด์กด

WordPress์—์„œ ์ƒ์„ฑ ํ•œ ๊ธฐ์กด SQL์„ ์œ ์ง€ํ•˜๋ ค๊ณ  ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค (์‹ฌ์ง€์–ด ๋“ค๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค). ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์•„๋งˆ๋„posts_request ํ•„ํ„ฐ๋ฅผ ์ฆ‰, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์ „์†ก๋˜๊ธฐ ์ง์ „์— ์™„์ „ํ•œ mySQL ์ฟผ๋ฆฌ์ด๋ฏ€๋กœ ์ž์‹ ์˜ ํ•„ํ„ฐ๋กœ ์™„์ „ํžˆ ํด๋กœ๋ฒ„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™œ ์ด๋ ‡๊ฒŒ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? ๋‹น์‹ ์€ ์•„๋งˆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ ˆ์—์„œ ๊ธฐ์กด SQL์„ ์œ ์ง€ํ•˜๋ ค๋ฉด ์ ˆ์— ์ถ”๊ฐ€ํ•˜์ง€ ๋ง๊ณ  ์ ˆ์— ํ• ๋‹นํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค (์˜ˆ : $join .= ' {NEW SQL STUFF}';not ์‚ฌ์šฉ) $join = '{CLOBBER SQL STUFF}';. $clauses๋ฐฐ์—ด ์˜ ๊ฐ ์š”์†Œ ๋Š” ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ๋‹ค๋ฅธ ๋ฌธ์ž ํ† ํฐ ์•ž์— ๊ณต๋ฐฑ์„ ์‚ฝ์ž…ํ•˜๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด SQL ๊ตฌ๋ฌธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ์ ˆ์— ํ•ญ์ƒ ๋ฌด์–ธ๊ฐ€๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ • ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณต๋ฐฑ์œผ๋กœ ์ƒˆ ๋ฌธ์ž์—ด์„ ๊ฐ๊ฐ ์‹œ์ž‘ $join .= ' my_tableํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์€ ์ค„์„ ํ•ญ์ƒ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$join = &$clauses['join'];
if (! empty( $join ) ) $join .= ' ';
$join .= "JOIN my_table... "; // <-- note the space at the end
$join .= "JOIN my_other_table... ";


return $clauses;

๊ทธ๊ฒƒ์€ ๋‹ค๋ฅธ ์–ด๋–ค ๊ฒƒ๋ณด๋‹ค ๋ฌธ์ฒด์ ์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ์–ตํ•ด์•ผ ํ•  ์ค‘์š”ํ•œ ์ ์€ ์ด๋ฏธ ์ผ๋ถ€ SQL์ด ํฌํ•จ ๋œ ์ ˆ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ๋ฌธ์ž์—ด ์•ž์— ๊ณต๋ฐฑ์„ ๋‚จ๊ฒจ ๋‘๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ

WordPress ๊ฐœ๋ฐœ์˜ ์ฒซ ๋ฒˆ์งธ ๊ทœ์น™์€ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.์ด๊ฒƒ์ด ํ–ฅํ›„ ์ž‘์—…์„ ์ฆ๋ช…ํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ•ต์‹ฌ ํŒ€์ด WordPress๊ฐ€ ์ด์ œ SQLite ๋˜๋Š” Oracle ๋˜๋Š” ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ผ๊ณ  ๊ฒฐ์ •ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์‹ญ์‹œ์˜ค. ์ง์ ‘ ์ž‘์„ฑํ•œ mySQL์ด ์œ ํšจํ•˜์ง€ ์•Š๊ฒŒ๋˜์–ด ํ”Œ๋Ÿฌ๊ทธ์ธ์ด๋‚˜ ํ…Œ๋งˆ๊ฐ€ ๊นจ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! WP๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋งŽ์€ SQL์„ ์ž์ฒด์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ํ•„์š”ํ•œ ๋น„ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋น„์ฆˆ๋‹ˆ์Šค ์šฐ์„  ์ˆœ์œ„๋Š” ์ตœ๋Œ€ํ•œ WP_Query๋งŽ์€ ๊ธฐ๋ณธ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑ ํ•˜๋Š” ๋ฐ ํ™œ์šฉ ๋ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์€์ด ๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก์ด ํ‘œ์‹œ ๋˜๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค . ํŽ˜์ด์ง€์˜ ํ•˜์œ„ ์„น์…˜ ์ธ ๊ฒฝ์šฐ (๊ธฐ๋ณธ ์ฟผ๋ฆฌ๊ฐ€ ์•„๋‹˜) get_posts(); ๊ธฐ๋ณธ ์ฟผ๋ฆฌ ์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ query_posts()ํ•˜๊ณ  ์™„๋ฃŒ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ ํ•˜์ง€๋งŒ ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์€ ๊ธฐ๋ณธ ์ฟผ๋ฆฌ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋„๋‹ฌํ•˜๊ธฐ ์ „์— ์ฐจ๋‹จํ•˜๊ณ  ์„œ๋ฒ„์ฃผ๊ธฐ๋ฅผ ์†Œ๋น„ํ•˜๋ฏ€๋กœ requestํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

์ด์ œ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ–ˆ์œผ๋ฉฐ SQL์„ ๋งŒ๋“ค๋ ค๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค, ๊ทธ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ณด๋‚ด์ง€์ง€ ์•Š๊ณ  ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. posts_clausesํ•„ํ„ฐ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง์› ๊ด€๊ณ„ ํ…Œ์ด๋ธ”์„ ๋ฏน์Šค์— ์ถ”๊ฐ€ํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์ด ํ…Œ์ด๋ธ”์„ {$ wpdb-> prefix}๋ผ๊ณ ํ•˜์ž. โ€˜user_relationshipโ€™์ด๋ฉฐ ๊ต์ฐจ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. (์ด๋Ÿฐ ์‹์œผ๋กœ์ด ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๊ณ  โ€˜relationship_idโ€™, โ€˜user_idโ€™, โ€˜related_user_idโ€™, โ€˜relationship_typeโ€™ํ•„๋“œ๊ฐ€์žˆ๋Š” ์ ์ ˆํ•œ ๊ต์ฐจ ํ…Œ์ด๋ธ”๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ›จ์”ฌ ์œ ์—ฐํ•˜๊ณ  ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค. .. ๊ทธ๋Ÿฌ๋‚˜ ๋‚˜๋Š” ์‚ฐ๋งŒํ•˜๋‹ค.)

๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์„ ์ดํ•ดํ•˜๋ฉด ๋ฆฌ๋”์˜ ID๋ฅผ ์ „๋‹ฌํ•œ ๋‹ค์Œ ํ•ด๋‹น ๋ฆฌ๋”์˜ ํŒ”๋กœ์›Œ๊ฐ€ ๊ฒŒ์‹œ ํ•œ ๊ฒŒ์‹œ๋ฌผ ๋งŒ ๋ณด๋ ค๊ณ ํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋Š” ๊ทธ ๊ถŒ๋ฆฌ๋ฅผ ์–ป์—ˆ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์ด ์˜ณ์ง€ ์•Š๋‹ค๋ฉด, ๋‚ด๊ฐ€ ๋งํ•œ ๊ฒƒ์„ ์ทจํ•˜์—ฌ ๊ทธ๊ฒƒ์„ ๋‹น์‹ ์˜ ํ•„์š”์— ๋งž๊ฒŒ ์กฐ์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ๊ณ ์ˆ˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค . a leader_id์™€ a๊ฐ€ follower_id์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ JOIN์€ {$wpdb->posts}.post_authorโ€˜user_relationshipโ€™ํ…Œ์ด๋ธ”์˜ โ€˜follower_idโ€™์— ๋Œ€ํ•œ ์™ธ๋ž˜ ํ‚ค๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

add_filter( 'posts_clauses', 'filter_by_leader_id', 10, 2 ); // we need the 2 because we want to get all the arguments

function filter_by_leader_id( $clauses, $query_object ){
  // I don't know how you intend to pass the leader_id, so let's just assume it's a global
  global $leader_id;

  // In this example I only want to affect a query on the home page.
  // This is where the $query_object is used, to help us avoid affecting
  // ALL queries (since ALL queries pass through this filter)
  if ( $query_object->is_home() ){
    // Now, let's add your table into the SQL
    $join = &$clauses['join'];
    if (! empty( $join ) ) $join .= ' '; // add a space only if we have to (for bonus marks!)
    $join .= "JOIN {$wpdb->prefix}employee_relationship EMP_R ON EMP_R.follower_id = {$wpdb->posts}.author_id";

    // And make sure we add it to our selection criteria
    $where = &$clauses['where'];
    // Regardless, you always start with AND, because there's always a '1=1' statement as the first statement of the WHERE clause that's added in by WP/
    // Just don't forget the leading space!
    $where .= " AND EMP_R.leader_id={$leader_id}"; // assuming $leader_id is always (int)

    // And I assume you'll want the posts "grouped" by user id, so let's modify the groupby clause
    $groupby = &$clauses['groupby'];
    // We need to prepend, so...
    if (! empty( $groupby ) ) $groupby = ' ' . $groupby; // For the show-offs
    $groupby = "{$wpdb->posts}.post_author" . $groupby;
  }

  // Regardless, we need to return our clauses...
  return $clauses;
}

๋‹ต๋ณ€

๋‚˜๋Š”์ด ์งˆ๋ฌธ์— ๋งค์šฐ ๋Šฆ๊ฒŒ ๋‹ต๋ณ€ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์‚ฌ๊ณผ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋งˆ๊ฐ์ผ์ด ๋„ˆ๋ฌด ๋ฐ”๋น ์„œ ์ฐธ์„ํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ™•์žฅํ•˜๊ณ  ๊ตฌํ˜„ํ•  ์ˆ˜์žˆ๋Š” ๊ธฐ๋ณธ ์†”๋ฃจ์…˜์„ ์ œ๊ณต ํ•œ @ m0r7if3r ๋ฐ @kaiser์—๊ฒŒ ํฐ ๊ฐ์‚ฌ๋ฅผ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์ด ๋‹ต๋ณ€์€ @ m0r7if3r ๋ฐ @kaiser๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์†”๋ฃจ์…˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๋จผ์ €, ์™œ์ด ์งˆ๋ฌธ์ด ์ฒ˜์Œ์— ์™”๋Š”์ง€ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ๊ณผ ๊ทธ ์˜๊ฒฌ์—์„œ WP_Query๊ฐ€ ์ฃผ์–ด์ง„ ์‚ฌ์šฉ์ž (ํŒ”๋กœ์–ด)๊ฐ€ ๋”ฐ๋ฅด๋Š” ๋ชจ๋“  ์‚ฌ์šฉ์ž (์ง€๋„์ž)๊ฐ€ ๊ฒŒ์‹œ๋ฌผ์„ ๊ฐ€์ ธ ์˜ค๋„๋ก ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”์ข…์ž์™€ ๋ฆฌ๋” ๊ฐ„์˜ ๊ด€๊ณ„๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค follow. ์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ํ•ด๊ฒฐ์ฑ…์€ ํŒ”๋กœ์–ด์˜ ๋ชจ๋“  ๋ฆฌ๋”์˜ ์‚ฌ์šฉ์ž ID๋ฅผ ํŒ”๋กœ์šฐ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€์ ธ ์™€์„œ ๋ฐฐ์—ด์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜๋ฅผ๋ณด์‹ญ์‹œ์˜ค :

global $wpdb;
$results = $wpdb->get_results($wpdb->prepare('SELECT leader_id FROM cs_follow WHERE follower_id = %s', $user_id));

foreach($results as $result)
    $leaders[] = $result->leader_id;

๋ฆฌ๋” ๋ฐฐ์—ด์ด ์žˆ์œผ๋ฉด WP_Query์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋ฅผ๋ณด์‹ญ์‹œ์˜ค :

if (isset($leaders)) $authors = implode(',', $leaders); // Necessary as authors argument of WP_Query only accepts string containing post author ID's seperated by commas

$args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 10,
    'author'            => $authors
);

$wp_query = new WP_Query( $args );

// Normal WordPress loop continues

์œ„์˜ ์†”๋ฃจ์…˜์€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ™•์žฅ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹ญ, ์ˆ˜์ฒœ ๋ช…์˜ ๋ฆฌ๋”๋ฅผ ๋”ฐ๋ฅด๋Š” ํŒ”๋กœ์›Œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ฆฌ๋” ID ๋ฐฐ์—ด์ด ๋งค์šฐ ์ปค์ ธ WordPress ์‚ฌ์ดํŠธ์—์„œ ๊ฐ ํŽ˜์ด์ง€๋กœ๋“œ์‹œ 100MB-250MB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ดํŠธ๊ฐ€ ์ถฉ๋Œํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ง์ ‘ SQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ด€๋ จ ๊ฒŒ์‹œ๋ฌผ์„ ๊ฐ€์ ธ ์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋•Œ @ m0r7if3r์˜ ์†”๋ฃจ์…˜์ด ๊ตฌํ•ด์กŒ์Šต๋‹ˆ๋‹ค. @kaiser์˜ ๊ถŒ์žฅ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋‘ ๊ตฌํ˜„์„ ๋ชจ๋‘ ํ…Œ์ŠคํŠธํ–ˆ์Šต๋‹ˆ๋‹ค. WordPress์˜ ์ƒˆ๋กœ์šด ํ…Œ์ŠคํŠธ ์„ค์น˜์— ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด CSV ํŒŒ์ผ์—์„œ ์•ฝ 47K ๋ช…์˜ ์‚ฌ์šฉ์ž๋ฅผ ๊ฐ€์ ธ ์™”์Šต๋‹ˆ๋‹ค. ์„ค์น˜ ์ค‘์— Twenty Eleven ํ…Œ๋งˆ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์•ฝ 50 ๋ช…์˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค๋ฅธ ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ๋”ฐ๋ฅด๋„๋ก for ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. @kaiser์™€ @ m0r7if3r์˜ ์†”๋ฃจ์…˜์— ๋Œ€ํ•œ ์ฟผ๋ฆฌ ์‹œ๊ฐ„์˜ ์ฐจ์ด๋Š” ์—„์ฒญ๋‚ฌ์Šต๋‹ˆ๋‹ค. @kaiser์˜ ์†”๋ฃจ์…˜์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ ์ฟผ๋ฆฌ๋งˆ๋‹ค ์•ฝ 2 ~ 5 ์ดˆ๊ฐ€ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ๋ณ€ํ˜•์€ WordPress๊ฐ€ ๋‚˜์ค‘์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ๋ฅผ ์บ์‹œ ํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด @ m0r7if3r์˜ ์†”๋ฃจ์…˜์€ ํ‰๊ท  0.02ms์˜ ์ฟผ๋ฆฌ ์‹œ๊ฐ„์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‘ ์†”๋ฃจ์…˜์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด leader_id ์—ด์— ๋Œ€ํ•ด ์ƒ‰์ธ์„ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์‹ฑ์ด ์—†์œผ๋ฉด ์ฟผ๋ฆฌ ์‹œ๊ฐ„์ด ํฌ๊ฒŒ ์ฆ๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

์–ด๋ ˆ์ด ๊ธฐ๋ฐ˜ ์†”๋ฃจ์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์€ ์•ฝ 100-150MB์ด๋ฉฐ ์ง์ ‘ SQL์„ ์‹คํ–‰ํ•  ๋•Œ 20MB๋กœ ๋–จ์–ด์กŒ์Šต๋‹ˆ๋‹ค.

์ถ”์ข…์ž ID๋ฅผ posts_where ํ•„ํ„ฐ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•ด์•ผ ํ•  ๋•Œ @ m0r7if3r์˜ ์†”๋ฃจ์…˜์œผ๋กœ ์ถฉ๋Œํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์•„๋Š” ๋ฐ”์— ๋”ฐ๋ฅด๋ฉด WordPress์—์„œ๋Š” ๋ณ€์ˆ˜๋ฅผ ํŒŒ์ผ๋Ÿฌ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ํ”ผํ•˜๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด WP_Query๋ฅผ ํ™•์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ๊ตฌํ˜„ ํ•œ ์ตœ์ข… ์†”๋ฃจ์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค (@ m0r7if3r์˜ ์†”๋ฃจ์…˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•จ).

class WP_Query_Posts_by_Leader extends WP_Query {
    var $follower_id;

    function __construct($args=array()) {
        if(!empty($args['follower_id'])) {
            $this->follower_id = $args['follower_id'];
            add_filter('posts_where', array($this, 'posts_where'));
        }

        parent::query($args);
    }

    function posts_where($where) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'follow';
        $where .= $wpdb->prepare(" AND post_author IN (SELECT leader_id FROM " . $table_name . " WHERE follower_id = %d )", $this->follower_id);
        return $where;
    }
}


$args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 10,
    'follower_id'       => $follower_id
);

$wp_query = new WP_Query_Posts_by_Leader( $args );

์ฐธ๊ณ  : ๊ฒฐ๊ตญ ๋‹ค์Œ ํ‘œ์— 120 ๋งŒ ํ•ญ๋ชฉ์œผ๋กœ ์œ„์˜ ์†”๋ฃจ์…˜์„ ์‹œ๋„ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ท  ์ฟผ๋ฆฌ ์‹œ๊ฐ„์€ ์•ฝ 0.060ms์˜€์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

posts_whereํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์™„์ „ํžˆ SQL ์†”๋ฃจ์…˜์œผ๋กœ์ด๋ฅผ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ๊ทธ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

if( some condition )
    add_filter( 'posts_where', 'wpse50305_leader_where' );
    // lol, question id is the same forward and backward

function wpse50305_leader_where( $where ) {
    $where .= $GLOBALS['wpdb']->prepare( ' AND post_author '.
        'IN ( '.
            'SELECT leader_id '.
            'FROM custom_table_name '.
            'WHERE follower_id = %s'.
        ' ) ', $follower_id );
    return $where;
}

๋‚˜๋Š” ์ด๊ฒƒ์„ ํ•  ์ˆ˜์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐ JOINํ•˜์ง€๋งŒ, ๋‚˜๋Š” ๊ทธ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋‚ผ ์ˆ˜ ์—†๋‹ค. ๋‚˜๋Š” ๊ทธ๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ๋†€๊ณ  ๊ณ„์† ๋Œ€๋‹ต์„ ์—…๋ฐ์ดํŠธ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋˜๋Š” @kaiser๊ฐ€ ์ œ์•ˆํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฆฌ๋”๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์ฟผ๋ฆฌํ•˜๋Š” ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ์ด๊ฒƒ์ด ๋œ ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ ํ™•์‹คํžˆ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ค‘์ฒฉ ๋œ SQL ์ฟผ๋ฆฌ๊ฐ€ ์ƒ๋‹นํžˆ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์–ด๋–ค ๋ฐฉ๋ฒ•์ด ๋” ๋‚˜์€์ง€ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ํšจ์œจ์„ฑ์„ ์ง์ ‘ ํ…Œ์ŠคํŠธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์„์—์„œ :

ํ•จ์ˆ˜๋ฅผ ๋„ฃ๊ณ  ๋ฉ”์†Œ๋“œ ๊ฐ€ ํ˜ธ์ถœ ๋˜๊ธฐ ์ง์ „์— functions.php์ˆ˜ํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค . ๋ฐ”๋กœ ๋‹ค์Œ ์— ๋‹ค๋ฅธ ์ฟผ๋ฆฌ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.add_filter()query()WP_Queryremove_filter()


๋‹ต๋ณ€

ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ

๋‘ ๊ธฐ๋Šฅ์„ functions.phpํŒŒ์ผ ์— ๋„ฃ์œผ์‹ญ์‹œ์˜ค . ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ฒซ ๋ฒˆ์งธ ๊ธฐ๋Šฅ์„ ์กฐ์ •ํ•˜๊ณ  ์‚ฌ์šฉ์ž ์ •์˜ ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฒฐ๊ณผ ๋ฐฐ์—ด ๋‚ด์—์„œ ํ˜„์žฌ ์‚ฌ์šฉ์ž ID๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ์‹œ๋„ / ์˜ค๋ฅ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค (์ฃผ์„ ์ฐธ์กฐ).

/**
 * Get "Leaders" of the current user
 * @param int $user_id The current users ID
 * @return array $query The leaders
 */
function wpse50305_get_leaders( $user_id )
{
    global $wpdb;

    return $wpdb->query( $wpdb->prepare(
        "
            SELECT `leader_id`, `follower_id`
            FROM %s
                WHERE `follower_id` = %s
            ORDERBY `leader_id` ASC
        ",
        // Edit the table name
        "{$wpdb->prefix}custom_table_name"
        $user_id
    ) );
}

/**
 * Get posts array that contain posts by
 * "Leaders" the current user is following
 * @return array $posts Posts that are by the current "Leader
 */
function wpse50305_list_posts_by_leader()
{
    get_currentuserinfo();
    global $current_user;

    $user_id = $current_user->ID;

    $leaders = wpse5035_get_leaders( $user_id );
    // could be that you need to loop over the $leaders
    // and get rid of the follower ids

    return get_posts( array(
        'author' => implode( ",", $leaders )
    ) );
}

ํ…œํ”Œ๋ฆฟ ๋‚ด๋ถ€

์—ฌ๊ธฐ์—์„œ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

foreach ( wpse50305_list_posts_by_leader() as $post )
{
    // do something with $post
}

์ฐธ๊ณ  ์šฐ๋ฆฌ๋Š” ๋ณธ๊ฑด (๊ฐ€) ์œ„์˜ ์ถ”์ธก ๊ฒŒ์ž„์˜ ์กฐ๊ธˆ ๊ทธ๋ž˜์„œ ๋“ฑ testdata๋กœ ์žˆ์Šต๋‹ˆ๋‹ค. ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค ๋‹น์‹ ์€ ์šฐ๋ฆฌ๊ฐ€ ๋‚˜์ค‘์— ๋…์ž๋“ค์—๊ฒŒ ๋งŒ์กฑ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ , ๊ทธ๋ž˜์„œ ๋‹น์‹ ์„ ์œ„ํ•ด ์ผํ•œ ๊ฒƒ์„์ด ๋Œ€๋‹ต์„ ํŽธ์ง‘ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ด๋‹น์ž๊ฐ€ ๋„ˆ๋ฌด ์ ์„ ๊ฒฝ์šฐ ์ˆ˜์ • ์‚ฌํ•ญ์„ ์Šน์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ์ด ๋ฉ”๋ชจ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌ.


๋‹ต๋ณ€

์ฐธ๊ณ  :์ด ๋‹ต๋ณ€์€ ์ฃผ์„์—์„œ ๊ธด ํ† ๋ก ์„ ํ”ผํ•˜๊ธฐ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

  1. ์ฃผ์„์˜ OP ์ฝ”๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์‚ฌ์šฉ์ž ์„ธํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์‚ฌ๋ก€๋กœ ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

    for ( $j = 2; $j <= 52; $j++ )
    {
        for ( $i = ($j + 1); $i <= 47000; $i++ )
        {
            $rows_affected = $wpdb->insert( $table_name, array( 'leader_id' => $i, 'follower_id' => $j ) );
        }
    }

    ํ…Œ์ŠคํŠธ ์ •๋ณด OP ์ด๋ฅผ ์œ„ํ•ด csv ํŒŒ์ผ์—์„œ ์•ฝ 47K ๋ช…์˜ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์ฒ˜์Œ 45 ๋ช…์˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค๋ฅธ ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ๋”ฐ๋ฅด๋„๋ก for ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

    • ๊ฒฐ๊ณผ์ ์œผ๋กœ 3,704,951 ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์‚ฌ์šฉ์ž ์ •์˜ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
    • ์ฒ˜์Œ์— @ m0r7if3r์˜ ์†”๋ฃจ์…˜์€ 95 ์ดˆ์˜ ์ฟผ๋ฆฌ ์‹œ๊ฐ„์„ ์ œ๊ณตํ•˜์—ฌ leader_id ์—ด์—์„œ ์ธ๋ฑ์‹ฑ์„ ์ผ  ํ›„ 0.020ms๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ์†Œ๋น„ ๋œ ์ด PHP ๋ฉ”๋ชจ๋ฆฌ๋Š” ์•ฝ 20MB์ž…๋‹ˆ๋‹ค.
    • ๋ฐ˜๋ฉด, ์ธ๋ฑ์‹ฑ์ด ์„ค์ •๋œ ์ฟผ๋ฆฌ์—์„œ ์†”๋ฃจ์…˜์€ ์•ฝ 2 ~ 5 ์ดˆ๊ฐ€ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ์†Œ๋น„ ๋œ ์ด PHP ๋ฉ”๋ชจ๋ฆฌ๋Š” ์•ฝ 117MB์ž…๋‹ˆ๋‹ค.
  2. ์ด ํ…Œ์ŠคํŠธ์— ๋Œ€ํ•œ ๋‚˜์˜ ๋Œ€๋‹ต :

    ๋ณด๋‹ค โ€œ์‹ค์ œโ€ํ…Œ์ŠคํŠธ : ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ a๋ฅผ ๋”ฐ๋ฅด๋„๋ก ํ•œ $leader_amount = rand( 0, 5 );๋‹ค์Œ $leader_amount x $random_ids = rand( 0, 47000 );๊ฐ ์‚ฌ์šฉ์ž ์˜ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค . ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ ์žˆ๋Š” ๊ฒƒ์€ : ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋กœ๋ฅผ ๋”ฐ๋ผ ๊ฐ€๋ฉด ๋‚ด ์†”๋ฃจ์…˜์ด ๋งค์šฐ ๋‚˜์  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€ : ํ…Œ์ŠคํŠธ๋ฅผ ์ˆ˜ํ–‰ ํ•œ ๋ฐฉ๋ฒ•๊ณผ ํƒ€์ด๋จธ๋ฅผ ์ •ํ™•ํžˆ ์ถ”๊ฐ€ ํ•œ ์œ„์น˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

    ๋˜ํ•œ ๋ฃจํ”„๋ฅผ ํ•จ๊ป˜ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— โ†‘ ์œ„์˜ ์‹œ๊ฐ„ ์ถ”์ ์€ ์‹ค์ œ๋กœ ์ธก์ • ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ์–ธ๊ธ‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฃจํ”„์—์„œ ๊ฒฐ๊ณผ ID ์„ธํŠธ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— ์ถ”๊ฐ€ ๊ณผ์ •


๋‹ต๋ณ€


์ด ๊ธ€์€ wordpress ์นดํ…Œ๊ณ ๋ฆฌ์— ๋ถ„๋ฅ˜๋˜์—ˆ๊ณ  ํƒœ๊ทธ๊ฐ€ ์žˆ์œผ๋ฉฐ ๋‹˜์— ์˜ํ•ด ์— ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.