How to improve the performance of a SQL query even after adding indexes? - sql

I am trying to execute the following sql query but it takes 22 seconds to execute. the number of returned items is 554192. I need to make this faster and have already put indexes in all the tables involved.
SELECT mc.name AS MediaName,
lcc.name AS Country,
i.overridedate AS Date,
oi.rating,
bl1.firstname + ' ' + bl1.surname AS Byline,
b.id BatchNo,
i.numinbatch ItemNumberInBatch,
bah.changedatutc AS BatchDate,
pri.code AS IssueNo,
pri.name AS Issue,
lm.neptunemessageid AS MessageNo,
lmt.name AS MessageType,
bl2.firstname + ' ' + bl2.surname AS SourceFullName,
lst.name AS SourceTypeDesc
FROM profiles P
INNER JOIN profileresults PR
ON P.id = PR.profileid
INNER JOIN items i
ON PR.itemid = I.id
INNER JOIN batches b
ON b.id = i.batchid
INNER JOIN itemorganisations oi
ON i.id = oi.itemid
INNER JOIN lookup_mediachannels mc
ON i.mediachannelid = mc.id
LEFT OUTER JOIN lookup_cities lc
ON lc.id = mc.cityid
LEFT OUTER JOIN lookup_countries lcc
ON lcc.id = mc.countryid
LEFT OUTER JOIN itembylines ib
ON ib.itemid = i.id
LEFT OUTER JOIN bylines bl1
ON bl1.id = ib.bylineid
LEFT OUTER JOIN batchactionhistory bah
ON b.id = bah.batchid
INNER JOIN itemorganisationissues ioi
ON ioi.itemorganisationid = oi.id
INNER JOIN projectissues pri
ON pri.id = ioi.issueid
LEFT OUTER JOIN itemorganisationmessages iom
ON iom.itemorganisationid = oi.id
LEFT OUTER JOIN lookup_messages lm
ON iom.messageid = lm.id
LEFT OUTER JOIN lookup_messagetypes lmt
ON lmt.id = lm.messagetypeid
LEFT OUTER JOIN itemorganisationsources ios
ON ios.itemorganisationid = oi.id
LEFT OUTER JOIN bylines bl2
ON bl2.id = ios.bylineid
LEFT OUTER JOIN lookup_sourcetypes lst
ON lst.id = ios.sourcetypeid
WHERE p.id = #profileID
AND b.statusid IN ( 6, 7 )
AND bah.batchactionid = 6
AND i.statusid = 2
AND i.isrelevant = 1
when looking at the execution plan I can see an step which is costing 42%. Is there any way I could get this to a lower threshold or any way that I can improve the performance of the whole query.

Remove the profiles table as it is not needed and change the WHERE clause to
WHERE PR.profileid = #profileID
You have a left outer join on the batchactionhistory table but also have a condition in your WHERE clause which turns it back into an inner join. Change you code to this:
LEFT OUTER JOIN batchactionhistory bah
ON b.id = bah.batchid
AND bah.batchactionid = 6
You don't need the batches table as it is used to join other tables which could be joined directly and to show the id in you SELECT which is also available in other tables. Make the following changes:
i.batchidid AS BatchNo,
LEFT OUTER JOIN batchactionhistory bah
ON i.batchidid = bah.batchid
Are any of the fields that are used in joins or the WHERE clause from tables that contain large amounts of data but are not indexed. If so try adding an index on at time to the largest table.
Do you need every field in the result - if you could loose one or to you maybe could reduce the number of tables further.

First, if this is not a stored procedure, make it one. That's a lot of text for sql server to complile.
Next, my experience is that "worst practices" are occasionally a good idea. Specifically, I have been able to improve performance by splitting large queries into a couple or three small ones and assembling the results.
If this query is associated with a .net, coldfusion, java, etc application, you might be able to do the split/re-assemble in your application code. If not, a temporary table might come in handy.

Related

I am converting following oracle query to bigquery but the result record counts are different

I am converting following oracle query to bigquery query but the results(record counts) are different, eventhough base tables involved in the query are having same number of records in both oracle and bq.
oracle :
SELECT
to_char(R_PROJECT_S.PROJECT_COPYRIGHT_YEAR),
R_PROJECT_S.PROJECT_TITLE,
to_char(R_PROJECT_S.EDITION),
R_PROJECT_S.CIRCULATION_DESC,
R_PROJECT_S.DISTRIBUTION_DESC,
R_PROJECT_S.PROJECT_ID,
DB.R_USAGE_INFO_S.OBJECT_ID,
UPPER(DB.R_INFO_S.PHOTOGRAPHER),
UPPER(DB.R_INFO_S.SOURCE_CAPTION),
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHEAU,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHECPYR,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHEED,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHEPRDDE,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHEGRDE,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHSODE,
R_PROJECT_S.CHARGE_TO_ISBN,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHEPTIT,
DB.R_INFO_S.SOURCE_NAME,
R_PROJECT_S.LANGUAGE_DESC,
R_PROJECT_S.PROJECT_FORMAT_DESC,
DB.R_USAGE_INFO_S.USAGE_ID,
DB.R_USAGE_INFO_S.PAGE,
DB.R_USAGE_INFO_S.CHAPTER,
DB.R_INFO_S.WORK_PROJECT_ID,
DB.R_INFO_S.IMAGE_TYPE_DESC,
DB.R_INFO_S.IMAGE_DESC,
DB.R_USAGE_INFO_S.PERMISSION_TYPE_DESC,
DB.R_USAGE_INFO_S.PERMISSION_STATUS_DESC,
DB.R_USAGE_INFO_S.PERMISSION_USAGE_DESC,
DB.R_USAGE_INFO_S.USAGE_LABEL,
DB.R_USAGE_INFO_S.QUOTED_COST,
DB.R_INFO_S.SOURCE_OBJECT_ID,
DB.R_USAGE_INFO_S.USAGE_TYPE_DESC,
GHEPM_TITLE_PSPP.TITLE_DESCRIPTION,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHESOAB,
ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHEGRCD
FROM
DB.R_PROJECT_S_VW R_PROJECT_S,
DB.R_USAGE_INFO_S,
DB.R_INFO_S,
ADMIN.BIC_APHEISBN00_BO_VW,
DB.GHEPM_TITLE GHEPM_TITLE_PSPP
WHERE
( R_PROJECT_S.PROJECT_ID=DB.R_USAGE_INFO_S.PROJECT_ID(+)
)
AND ( DB.R_USAGE_INFO_S.OBJECT_ID=DB.R_INFO_S.OBJECT_ID )
AND ( R_PROJECT_S.PROJECT_ID=ADMIN.BIC_APHEISBN00_BO_VW.BIC_ZCHETIIS(+) )
AND ( R_PROJECT_S.PROJECT_ID=DB.GHEPM_TITLE_PSPP.ISBN10(+) )
AND UPPER(DB.R_USAGE_INFO_S.USAGE_LABEL) NOT LIKE UNISTR('%KILL%')
BQ:
SELECT
CAST(R_PROJECT_S.PROJECT_COPYRIGHT_YEAR AS string) COPYRIGHT_YEAR,
R_PROJECT_S.PROJECT_TITLE,
CAST(R_PROJECT_S.EDITION AS string) EDITION,
R_PROJECT_S.CIRCULATION_DESC,
R_PROJECT_S.DISTRIBUTION_DESC,
R_PROJECT_S.PROJECT_ID,
R_USAGE_INFO_S.OBJECT_ID,
UPPER(R_INFO_S.PHOTOGRAPHER) PHOTOGRAPHER,
UPPER(R_INFO_S.SOURCE_CAPTION) SOURCE_CAPTION,
BIC_APHEISBN00_BO._BIC_ZCHEAU,
BIC_APHEISBN00_BO._BIC_ZCHECPYR,
BIC_APHEISBN00_BO._BIC_ZCHEED,
BIC_APHEISBN00_BO._BIC_ZCHEPRDDE,
BIC_APHEISBN00_BO._BIC_ZCHEGRDE,
BIC_APHEISBN00_BO._BIC_ZCHSODE,
R_PROJECT_S.CHARGE_TO_ISBN,
BIC_APHEISBN00_BO._BIC_ZCHEPTIT,
R_INFO_S.SOURCE_NAME,
R_PROJECT_S.LANGUAGE_DESC,
R_PROJECT_S.PROJECT_FORMAT_DESC,
R_USAGE_INFO_S.USAGE_ID,
R_USAGE_INFO_S.PAGE,
R_USAGE_INFO_S.CHAPTER,
R_INFO_S.WORK_PROJECT_ID,
R_INFO_S.IMAGE_TYPE_DESC,
R_INFO_S.IMAGE_DESC,
R_USAGE_INFO_S.PERMISSION_TYPE_DESC,
R_USAGE_INFO_S.PERMISSION_STATUS_DESC,
R_USAGE_INFO_S.PERMISSION_USAGE_DESC,
R_USAGE_INFO_S.USAGE_LABEL,
R_USAGE_INFO_S.QUOTED_COST,
R_INFO_S.SOURCE_OBJECT_ID,
R_USAGE_INFO_S.USAGE_TYPE_DESC,
GHEPM_TITLE_PSPP.TITLE_DESCRIPTION,
BIC_APHEISBN00_BO._BIC_ZCHESOAB,
BIC_APHEISBN00_BO._BIC_ZCHEGRCD
FROM
`domain-rr.oracle_DB_DB.R_info_s` R_INFO_S
inner join
`domain-rr.oracle_DB_DB.R_usage_info_s` R_USAGE_INFO_S
on
R_USAGE_INFO_S.OBJECT_ID=R_INFO_S.OBJECT_ID
right outer join
`domain-rr.DB_RPT.R_PROJECT_S_VW` R_PROJECT_S
on
R_PROJECT_S.PROJECT_ID=R_USAGE_INFO_S.PROJECT_ID
left outer join
`domain-rr.DB_RPT.BIC_APHEISBN00_BO_VW` BIC_APHEISBN00_BO
ON
R_PROJECT_S.PROJECT_ID=BIC_APHEISBN00_BO._BIC_ZCHETIIS
left outer join
`domain-rr.oracle_DB_DB.ghepm_title` GHEPM_TITLE_PSPP
ON
R_PROJECT_S.PROJECT_ID=GHEPM_TITLE_PSPP.ISBN10
AND UPPER(R_USAGE_INFO_S.USAGE_LABEL) NOT LIKE '%KILL%'
Oracle count - 1553437
BQ count - 2414413
Please help me on how to get counts are same on both oracle and bq
Thanks,
Naren
Had you used more readable, shortened table aliases several differences can be illuminated:
Oracle does not attempt any RIGHT JOIN;
GBQ should run UPPER(...) expression in WHERE not on last LEFT JOIN clause or move expression to INNER JOIN on ui table (but without testing may not make a difference but readability);
Table order may make a difference especially with use of both INNER and OUTER joins;
Oracle (using the older, outdated implicit joins)
...
FROM
GRDW.RMS_IMAGE_PROJECT_S_VW p,
GRDW.RMS_IMAGE_USAGE_INFO_S ui,
GRDW.RMS_IMAGE_INFO_S i,
BOADMIN.BIC_APHEISBN00_BO_VW b,
GRDW.GHEPM_TITLE g
WHERE
( p.PROJECT_ID = ui.PROJECT_ID(+) -- LEFT JOIN
)
AND ( ui.OBJECT_ID = i.OBJECT_ID ) -- INNER JOIN
AND ( p.PROJECT_ID = b.BIC_ZCHETIIS(+) ) -- LEFT JOIN
AND ( p.PROJECT_ID = g.ISBN10(+) ) -- LEFT JOIN
AND UPPER(ui.USAGE_LABEL) NOT LIKE UNISTR('%KILL%')
Google BigQuery (using current standard of explicit joins)
...
FROM
`pearson-rr.oracle_grdw_grdw.rms_image_info_s` i
INNER JOIN
`pearson-rr.oracle_grdw_grdw.rms_image_usage_info_s` ui
ON ui.OBJECT_ID = i.OBJECT_ID
RIGHT OUTER JOIN
`pearson-rr.GRDW_RPT.RMS_IMAGE_PROJECT_S_VW` p
ON p.PROJECT_ID = ui.PROJECT_ID
LEFT OUTER JOIN
`pearson-rr.GRDW_RPT.BIC_APHEISBN00_BO_VW` b
ON p.PROJECT_ID = b._BIC_ZCHETIIS
LEFT OUTER JOIN
`pearson-rr.oracle_grdw_grdw.ghepm_title` g
ON p.PROJECT_ID = g.ISBN10
AND UPPER(ui.USAGE_LABEL) NOT LIKE '%KILL%'
Therefore, to account for table order and appropriate JOIN, consider below adjusted Google BigQuery:
...
FROM
`pearson-rr.GRDW_RPT.RMS_IMAGE_PROJECT_S_VW` p
LEFT OUTER JOIN
`pearson-rr.oracle_grdw_grdw.rms_image_usage_info_s` ui
ON p.PROJECT_ID = ui.PROJECT_ID
INNER OUTER JOIN
`pearson-rr.oracle_grdw_grdw.rms_image_info_s` i
ON ui.OBJECT_ID = i.OBJECT_ID AND UPPER(ui.USAGE_LABEL) NOT LIKE '%KILL%'
LEFT OUTER JOIN
`pearson-rr.GRDW_RPT.BIC_APHEISBN00_BO_VW` b
ON p.PROJECT_ID = b._BIC_ZCHETIIS
LEFT OUTER JOIN
`pearson-rr.oracle_grdw_grdw.ghepm_title` g
ON p.PROJECT_ID = g.ISBN10

Access 'Join not supported' Moving LEFT Join

I've done some research here and I understand that in Access nested joins cause issues.
I believe that is the issue in the first example.
SELECT
Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((
Recipe_Classes
LEFT JOIN Recipes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID)
INNER JOIN Recipe_Ingredients
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)
INNER JOIN Ingredients
ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID)
INNER JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID
ORDER BY RecipeTitle, RecipeSeqNo;
I made an attempt to remove the nesting and created a right join in this example
SELECT
Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((
Ingredients
INNER JOIN Recipe_Ingredients
ON Ingredeints.IngredientID = Recipe_Ingredients.IngredientID)
INNER JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID)
INNER JOIN Recipes
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)
RIGHT JOIN Recipe_Classes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID
ORDER BY RecipeTitle, RecipeSeqNo;
Can anyone point me in the right direction?
The issue may be the left join that is not needed. Try this from clause:
SELECT Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((Recipes LEFT JOIN
Recipe_Classes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID
) LEFT JOIN
Recipe_Ingredients
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID
) LEFT JOIN
Ingredients
ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID
) LEFT JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID
ORDER BY RecipeTitle, RecipeSeqNo;
Once you start with LEFT JOINs, generally all the remaining joins should also be LEFT JOINs.
Since your main focal point table is Recipe_Ingredients, consider starting with this table in FROM and then JOIN the other parent tables and even nest the Recipes and Recipe_Classes pair:
SELECT
c.RecipeClassDescription,
r.RecipeTitle,
r.Preparation,
i.IngredientName,
ri.RecipeSeqNo,
ri.Amount,
m.MeasurementDescription
FROM
((Recipe_Ingredients ri
INNER JOIN (Recipes r
RIGHT JOIN Recipe_Classes c
ON c.RecipeClassID = r.RecipeClassID)
ON r.RecipeID = ri.RecipeID)
INNER JOIN Ingredients i
ON i.IngredientID = ri.IngredientID)
INNER JOIN Measurements m
ON m.MeasureAmountID = ri.MeasureAmountID
ORDER BY r.RecipeTitle, ri.RecipeSeqNo;
Of course this is untested without data. Due to Access' JOIN requirements like parnetheses, it is often recommended for new users to build complex queries with MS Access' GUI Query Design then modify generated SQL as needed.

Why my MariaDB query takes so long? How can I fix it?

SELECT
ps_customer.id_customer
CONCAT(ps_customer.firstname," ",ps_customer.lastname) AS "Fullname"
ps_customer.email
ps_customer.date_add
ps_orders.id_order
ps_orders.id_address_delivery
ps_orders.payment
ps_stripepro_transaction.status
ps_order_detail.id_shop
ps_order_detail.product_name
ps_address.address1
ps_address.address2
ps_address.postcode
ps_address.city
ps_address.phone
ps_address.phone_mobile
ps_vd_customer_dogs.name
ps_vd_customer_dogs.size
ps_vd_customer_dogs.breed
ps_vd_customer_dogs.toy_destroyer
ps_order_cart_rule.name
ps_order_cart_rule.value
ps_order_detail.product_quantity
ps_message.message
FROM ps_orders
LEFT JOIN ps_customer
ON ps_orders.id_customer = ps_customer.id_customer
LEFT JOIN ps_stripepro_transaction
ON ps_orders.id_order = ps_stripepro_transaction.id_order
LEFT JOIN ps_order_detail
ON ps_orders.id_order = ps_order_detail.id_order
LEFT JOIN ps_address
ON ps_orders.id_customer = ps_address.id_customer
LEFT JOIN ps_vd_customer_dogs
ON ps_orders.id_customer = ps_vd_customer_dogs.id_customer
LEFT JOIN ps_order_cart_rule
ON ps_orders.id_order = ps_order_cart_rule.id_order
LEFT JOIN ps_message
ON ps_orders.id_customer = ps_message.id_customer
ORDER BY ps_orders.id_order DESC
This is my query. If I shorten it and reduce it to the first 4 left joins, I get the results in 10 min, but when I add the other joins I have to wait over 3 hours.
How can I improve this? My objective is to create a CSV with all the data I'm selecting so that we can improve many people's job in my office who have to check many parameters for each client in our PrestaShop backoffice.

Postgresql - Conditional Join if data exist

My current query show the data from the table called "Buque" and has some references from another tables. The problem is when i execute the query it never shows the result because it consumes too much memory i guess.
The current query i have
select buq.buq_codigo, tbu.tbu_codigo, tbu.tbu_nombre, pai.pai_codigo, pai.pai_nombre,
pue.pto_codigo, pue.pto_nombre, lin.lin_codigo, lin.lin_nombre, tra.tra_codigo,
tra.tra_nombre, buq.buq_nombre, buq.buq_des, buq.num_trb, buq.num_eslora,
buq.max_tons, buq.reg_lloyd, buq.buq_codigo1, buq.codigo_omi,
case buq.buq_estado when 'A' then 'Activo' else 'Inactivo' end as buq_estado
from publico.mae_buque as buq, publico.mae_tipbuque as tbu, publico.mae_pais as pai,
publico.mae_puerto as pue, publico.mae_linea as lin, publico.mae_trafico as tra
where buq.tbu_codigo = tbu.tbu_codigo or
buq.pai_codigo = pai.pai_codigo or
buq.pto_codigo = pue.pto_codigo or
buq.lin_codigo = lin.lin_codigo or
buq.tra_codigo = tra.tra_codigo
I also tried with inner joins but the problem is it returns me the data that meets the conditions of the joins. In other words, if the join has data to compare, returns the name, if not, show the null data.
The query must return me 611 records, with inner joins returns 68 records.
Concerning your desired result, use left outer joins, which fill up any non-existing rows of the right hand side table with null-values;
Concerning the out of memory issue, note that you used or to connect your tables; this actually leads to the fact that almost every record of the involved tables is connected to almost every other record (almost a cross join / cartesian product); This can get very large if you connect 6 tables...
select buq.buq_codigo, tbu.tbu_codigo, tbu.tbu_nombre, pai.pai_codigo, pai.pai_nombre,
pue.pto_codigo, pue.pto_nombre, lin.lin_codigo, lin.lin_nombre, tra.tra_codigo,
tra.tra_nombre, buq.buq_nombre, buq.buq_des, buq.num_trb, buq.num_eslora,
buq.max_tons, buq.reg_lloyd, buq.buq_codigo1, buq.codigo_omi,
case buq.buq_estado when 'A' then 'Activo' else 'Inactivo' end as buq_estado
from publico.mae_buque as buq
left outer join publico.mae_tipbuque as tbu on buq.tbu_codigo = tbu.tbu_codigo
left outer join publico.mae_pais as pai on (buq.pai_codigo = pai.pai_codigo)
left outer join publico.mae_puerto as pue on (buq.pto_codigo = pue.pto_codigo)
left outer join publico.mae_linea as lin on (buq.lin_codigo = lin.lin_codigo)
left outer join publico.mae_trafico as tra on (buq.tra_codigo = tra.tra_codigo)
You have to use left outer join:
select *
from
publico.mae_buque as buq
left outer join publico.mae_tipbuque as tbu on (buq.tbu_codigo = tbu.tbu_codigo)
left outer join publico.mae_pais as pai on (buq.pai_codigo = pai.pai_codigo)
left outer join publico.mae_puerto as pue on (buq.pto_codigo = pue.pto_codigo )
left outer join publico.mae_linea as lin on (buq.lin_codigo = lin.lin_codigo)
left outer join publico.mae_trafico as tra on (buq.tra_codigo = tra.tra_codigo);

Reduce query complexity

I have this multi join query, is there any way to reduce the number of joins? Or maybe to split the query to 2 parts but then still get the same result set?
Having too many joins makes the query execute very slowly and inefficently
SELECT
MD.EVENT_ID,
AUI.ATLAS_USER_ID,
EVENT_TIME,
EVENT_TYPE,
INTERACTION_TOKEN,
CC.COUNTRY_CODE,
AP.ADV_PROJECT_ID, AP.ADV_PROJECT_NAME,
AC.ADV_CAMPAIGN_ID,
ADV_CAMPAIGN_NAME,
PC.PRT_CAMPAIGN_ID, PC.PRT_CAMPAIGN_NAME,
IP.IP_ADDRESS,
OS.OS,
BR.BROWSER,
FU.FULL_USER_AGENT,
RAW_ACTION_ID,
SE.SELLER_NETWORK_ID,
RIURL.RAW_INPUT as URL,
RIREF.RAW_INPUT as REF_URL,
MVG1.MAP_VALUE as TABOOLA ,
MVG2.MAP_VALUE as APPNEXUS ,
MVG3.MAP_VALUE as ETAG ,
MVG4.MAP_VALUE as FACEBOOK ,
MVG5.MAP_VALUE as MEDIAMATH ,
MVG6.MAP_VALUE as COOKIEID ,
MVG7.MAP_VALUE as IDFA ,
MVG8.MAP_VALUE as ADVLOGIN ,
MVG9.MAP_VALUE as OPENX ,
MVG10.MAP_VALUE as ADTRUTH ,
MVG11.MAP_VALUE as GOOGLE ,
MVG12.MAP_VALUE as ANDROID_ADV_ID ,
MVG13.MAP_VALUE as SDGUPI ,
MVG15.MAP_VALUE as RMX
FROM
EVENT_124_2 BASE
INNER JOIN
atlas__atlas_events MD ON BASE.EVENT_ID = MD.EVENT_ID
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG1 ON MVG1.MAP_VALUE_GK = BASE.TABOOLA_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG2 ON MVG2.MAP_VALUE_GK = BASE.APPNEXUS_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG3 ON MVG3.MAP_VALUE_GK = BASE.ETAG_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG4 ON MVG4.MAP_VALUE_GK = BASE.FACEBOOK_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG5 ON MVG5.MAP_VALUE_GK = BASE.MEDIAMATH_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG6 ON MVG6.MAP_VALUE_GK = BASE.COOKIEID_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG7 ON MVG7.MAP_VALUE_GK = BASE.IDFA_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG8 ON MVG8.MAP_VALUE_GK = BASE.ADVLOGIN_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG9 ON MVG9.MAP_VALUE_GK = BASE.OPENX_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG10 ON MVG10.MAP_VALUE_GK = BASE.ADTRUTH_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG11 ON MVG11.MAP_VALUE_GK = BASE.GOOGLE_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG12 ON MVG12.MAP_VALUE_GK = BASE.ANDROID_ADV_ID_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG13 ON MVG13.MAP_VALUE_GK = BASE.SDGUPI_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG15 ON MVG15.MAP_VALUE_GK = BASE.RMX_HASH
LEFT JOIN
LOOKUP_STG__ATLAS_USER_ID AUI ON AUI.ATLAS_USER_ID_GK = MD.ATLAS_USER_ID
LEFT JOIN
GAYA__ADV_PROJECTS AP ON AP.ADV_PROJECT_ID = MD.ADV_PROJECT_ID
LEFT JOIN
GAYA__ADV_CAMPAIGNS AC ON AC.ADV_CAMPAIGN_ID = MD.ADV_CAMPAIGN_ID
LEFT JOIN
GAYA__PRT_CAMPAIGNS PC ON PC.PRT_CAMPAIGN_ID = MD.PRT_CAMPAIGN_ID
LEFT JOIN
LOOKUP_STG__RAW_INPUT RIURL ON RIURL.RAW_INPUT_GK = BASE.URL_HASH
LEFT JOIN
LOOKUP_STG__RAW_INPUT RIREF ON RIREF.RAW_INPUT_GK = BASE.REF_HASH
LEFT JOIN
LOOKUP_STG__OS OS ON OS.OS_GK = MD.COUNTRY_CODE
LEFT JOIN
LOOKUP_STG__COUNTRY_CODE CC ON CC.COUNTRY_CODE_GK = MD.COUNTRY_CODE
LEFT JOIN
LOOKUP_STG__BROWSER BR ON BR.BROWSER_GK = MD.BROWSER
LEFT JOIN
LOOKUP_STG__IP_ADDRESS IP ON IP.IP_ADDRESS_GK = MD.IP_ADDRESS
LEFT JOIN
LOOKUP_STG__SELLER_NETWORK_ID SE ON SE.SELLER_NETWORK_ID_GK = MD.SELLER_NETWORK_ID
LEFT JOIN
LOOKUP_STG__FULL_USER_AGENT FU ON FU.FULL_USER_AGENT_GK = MD.FULL_USER_AGENT
;
Do you have any indexes on your key columns? You might try to put bitmap indexes on each key column in the main fact table and also in the dimension tables. That should give you a considerable speedup.
You say that the number of joins makes the query inefficient. But what you should be looking at is whether the tables that are joined with have the proper indexes in them. You obviously need all those fields from the tables you join with, so you can't simply not join with them.
For every JOIN you make in the query, verify that there exists an INDEX on the field that is joined with. For instance:
INNER JOIN
atlas__atlas_events MD ON BASE.EVENT_ID = MD.EVENT_ID
Does the table atlas__atlas_events have an INDEX on the EVENT_ID column?
You need to verify this for every such JOIN in your query. If such an INDEX does not exist you should create one.
If you execute this query in SQL Server Management Studio and include the actual execution plan, you will probably already see indications that you are missing indexes.