Skip to content

Commit

Permalink
Use UNION ALL for performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syfaro committed Apr 22, 2021
1 parent ca2247b commit 34d3282
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 73 deletions.
140 changes: 70 additions & 70 deletions fuzzysearch/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,74 @@
]
}
},
"3d87281c06f5fe0dddc766d8321be22c2e4e369fa3f8d657c0b17ff354e214d6": {
"query": "SELECT\n 'FurAffinity' site,\n submission.id,\n submission.hash_int hash,\n submission.url,\n submission.filename,\n ARRAY(SELECT artist.name) artists,\n submission.file_id,\n null sources,\n submission.rating\n FROM submission\n JOIN artist ON submission.artist_id = artist.id\n WHERE hash_int <@ ($1, 0)\n UNION ALL\n SELECT\n 'e621' site,\n e621.id,\n e621.hash,\n e621.data->'file'->>'url' url,\n (e621.data->'file'->>'md5') || '.' || (e621.data->'file'->>'ext') filename,\n ARRAY(SELECT jsonb_array_elements_text(e621.data->'tags'->'artist')) artists,\n null file_id,\n ARRAY(SELECT jsonb_array_elements_text(e621.data->'sources')) sources,\n e621.data->>'rating' rating\n FROM e621\n WHERE hash <@ ($1, 0)\n UNION ALL\n SELECT\n 'Weasyl' site,\n weasyl.id,\n weasyl.hash,\n weasyl.data->>'link' url,\n null filename,\n ARRAY(SELECT weasyl.data->>'owner_login') artists,\n null file_id,\n null sources,\n weasyl.data->>'rating' rating\n FROM weasyl\n WHERE hash <@ ($1, 0)\n UNION ALL\n SELECT\n 'Twitter' site,\n tweet.id,\n tweet_media.hash,\n tweet_media.url,\n null filename,\n ARRAY(SELECT tweet.data->'user'->>'screen_name') artists,\n null file_id,\n null sources,\n CASE\n WHEN (tweet.data->'possibly_sensitive')::boolean IS true THEN 'adult'\n WHEN (tweet.data->'possibly_sensitive')::boolean IS false THEN 'general'\n END rating\n FROM tweet_media\n JOIN tweet ON tweet_media.tweet_id = tweet.id\n WHERE hash <@ ($1, 0)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "site",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "id",
"type_info": "Int8"
},
{
"ordinal": 2,
"name": "hash",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "url",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "filename",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "artists",
"type_info": "TextArray"
},
{
"ordinal": 6,
"name": "file_id",
"type_info": "Int4"
},
{
"ordinal": 7,
"name": "sources",
"type_info": "TextArray"
},
{
"ordinal": 8,
"name": "rating",
"type_info": "Bpchar"
}
],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": [
null,
null,
null,
null,
null,
null,
null,
null,
null
]
}
},
"659ee9ddc1c5ccd42ba9dc1617440544c30ece449ba3ba7f9d39f447b8af3cfe": {
"query": "SELECT\n api_key.id,\n api_key.name_limit,\n api_key.image_limit,\n api_key.hash_limit,\n api_key.name,\n account.email owner_email\n FROM\n api_key\n JOIN account\n ON account.id = api_key.user_id\n WHERE\n api_key.key = $1\n ",
"describe": {
Expand Down Expand Up @@ -74,12 +142,12 @@
{
"ordinal": 4,
"name": "name",
"type_info": "Text"
"type_info": "Varchar"
},
{
"ordinal": 5,
"name": "owner_email",
"type_info": "Text"
"type_info": "Varchar"
}
],
"parameters": {
Expand Down Expand Up @@ -116,73 +184,5 @@
null
]
}
},
"f798404b69897f47a732fb6a8e08e843f233803aea5d5a7f04087619dbb55626": {
"query": "SELECT\n 'FurAffinity' site,\n submission.id,\n submission.hash_int hash,\n submission.url,\n submission.filename,\n ARRAY(SELECT artist.name) artists,\n submission.file_id,\n null sources,\n submission.rating\n FROM submission\n JOIN artist ON submission.artist_id = artist.id\n WHERE hash_int <@ ($1, 0)\n UNION\n SELECT\n 'e621' site,\n e621.id,\n e621.hash,\n e621.data->'file'->>'url' url,\n (e621.data->'file'->>'md5') || '.' || (e621.data->'file'->>'ext') filename,\n ARRAY(SELECT jsonb_array_elements_text(e621.data->'tags'->'artist')) artists,\n null file_id,\n ARRAY(SELECT jsonb_array_elements_text(e621.data->'sources')) sources,\n e621.data->>'rating' rating\n FROM e621\n WHERE hash <@ ($1, 0)\n UNION\n SELECT\n 'Weasyl' site,\n weasyl.id,\n weasyl.hash,\n weasyl.data->>'link' url,\n null filename,\n ARRAY(SELECT weasyl.data->>'owner_login') artists,\n null file_id,\n null sources,\n weasyl.data->>'rating' rating\n FROM weasyl\n WHERE hash <@ ($1, 0)\n UNION\n SELECT\n 'Twitter' site,\n tweet.id,\n tweet_media.hash,\n tweet_media.url,\n null filename,\n ARRAY(SELECT tweet.data->'user'->>'screen_name') artists,\n null file_id,\n null sources,\n CASE\n WHEN (tweet.data->'possibly_sensitive')::boolean IS true THEN 'adult'\n WHEN (tweet.data->'possibly_sensitive')::boolean IS false THEN 'general'\n END rating\n FROM tweet_media\n JOIN tweet ON tweet_media.tweet_id = tweet.id\n WHERE hash <@ ($1, 0)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "site",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "id",
"type_info": "Int8"
},
{
"ordinal": 2,
"name": "hash",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "url",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "filename",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "artists",
"type_info": "TextArray"
},
{
"ordinal": 6,
"name": "file_id",
"type_info": "Int4"
},
{
"ordinal": 7,
"name": "sources",
"type_info": "TextArray"
},
{
"ordinal": 8,
"name": "rating",
"type_info": "Bpchar"
}
],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": [
null,
null,
null,
null,
null,
null,
null,
null,
null
]
}
}
}
6 changes: 3 additions & 3 deletions fuzzysearch/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn image_query_sync(
FROM submission
JOIN artist ON submission.artist_id = artist.id
WHERE hash_int <@ ($1, 0)
UNION
UNION ALL
SELECT
'e621' site,
e621.id,
Expand All @@ -129,7 +129,7 @@ pub fn image_query_sync(
e621.data->>'rating' rating
FROM e621
WHERE hash <@ ($1, 0)
UNION
UNION ALL
SELECT
'Weasyl' site,
weasyl.id,
Expand All @@ -142,7 +142,7 @@ pub fn image_query_sync(
weasyl.data->>'rating' rating
FROM weasyl
WHERE hash <@ ($1, 0)
UNION
UNION ALL
SELECT
'Twitter' site,
tweet.id,
Expand Down

0 comments on commit 34d3282

Please sign in to comment.