Snippet Name: MySQL_Aurora_Performance_Query
Tags:
Created Date: 2025-06-16 13:12:43
Last Modified Date: 2025-05-31 10:32:10
Dependencies:
DBA Notes:
Sample Output:
Title: MySQL Aurora Performance Query
Author: user
my Msql on AWS Aurora, i have a script i wnat to test for performance. it has parameters, and i am having trouble getting this syntax to work. can you see my issue: SET p_customer_business_phone='';
SET p_customer_cell_phone='9097679493';
SET p_customer_home_phone='9097679493';
SET p_customer_main_phone='';
SET p_customer_personal_email='';
SET p_customer_work_email='';
SET p_customer_main_email='SAMANTHALHURD@AOL.COM';
SET p_hyperion='2638';
SET p_vin='3GCNAAEK1PG341142';
SET p_year='2023';
SET p_make='CHEV';
SET p_model='SILV15';
with phones as (
select nullif(p_customer_business_phone, '') as phone_no union
select nullif(p_customer_cell_phone, '') union
select nullif(p_customer_home_phone, '') union
select nullif(p_customer_main_phone, '')
), emails as (
select nullif(p_customer_personal_email, '') as email_address union
select nullif(p_customer_work_email, '') union
select nullif(p_customer_main_email, '')
)
select l.*
from Spels.Lead l
join Spels.Vehicle v on l.lead_id = v.lead_id
and l.hyperion_code = p_hyperion
and ( (v.vin = p_vin ) or (v.year = p_year
and v.make = p_make and v.model = p_model ) )
and l.active_flg = 1
join (
select c.customer_id, c.last_name
from Spels.Customer c
join Spels.Customer_Email ce on c.customer_id = ce.customer_id
join emails e on ce.email_address = e.email_address
where c.last_name = coalesce(nullif(p_customer_last_name , ''), c.last_name )
union
select c.customer_id, c.last_name
from Spels.Customer c
join Spels.Customer_Phone cp on c.customer_id = cp.customer_id
join phones p on Spels.fn_PhoneNumber(p.phone_no) = Spels.fn_PhoneNumber(cp.phone_no)
where c.last_name = coalesce(nullif(p_customer_last_name , ''), c.last_name )
) contact on l.customer_id = contact.customer_id
Author: ChatGPT
There are a few syntax issues in your query that might be causing errors in MySQL on AWS Aurora:
### **Issues and Fixes**
1. **Variable Declaration & Usage (`SET` vs. `@variable`)**
- In MySQL, when setting session variables, you should use `@` before the variable names. Instead of:
```sql
SET p_customer_business_phone='';
```
Use:
```sql
SET @p_customer_business_phone='';
```
- Likewise, replace all instances of `p_variable_name` with `@p_variable_name` throughout your query.
2. **Referencing Variables in Queries**
- When using session variables in queries, you should reference them as `@variable_name`:
```sql
select nullif(@p_customer_business_phone, '') as phone_no
```
Replace all instances of `p_customer_business_phone` (and other parameters) accordingly.
3. **Missing `p_customer_last_name` Declaration**
- You're using `p_customer_last_name`, but it's not set anywhere. Define it at the top with:
```sql
SET @p_customer_last_name = '';
```
Or remove the `coalesce(nullif(@p_customer_last_name , ''), c.last_name )` condition if unnecessary.
4. **Fix `fn_PhoneNumber` Function Call**
- The `fn_PhoneNumber` function is being called as:
```sql
join phones p on Spels.fn_PhoneNumber(p.phone_no) = Spels.fn_PhoneNumber(cp.phone_no)
```
- Since `p.phone_no` comes from a **Common Table Expression (CTE)**, ensure the function can process `NULL` values safely.
5. **Common Table Expressions (CTEs) & MySQL Version**
- Ensure your MySQL version supports CTEs (`WITH` statements). AWS Aurora MySQL 5.7 **does not** support them, but MySQL 8.0 does.
- If using MySQL 5.7, rewrite the query using temporary tables or subqueries instead of `WITH`.
---
### **Revised Query**
```sql
SET @p_customer_business_phone='';
SET @p_customer_cell_phone='9097679493';
SET @p_customer_home_phone='9097679493';
SET @p_customer_main_phone='';
SET @p_customer_personal_email='';
SET @p_customer_work_email='';
SET @p_customer_main_email='SAMANTHALHURD@AOL.COM';
SET @p_hyperion='2638';
SET @p_vin='3GCNAAEK1PG341142';
SET @p_year='2023';
SET @p_make='CHEV';
SET @p_model='SILV15';
SET @p_customer_last_name = '';
WITH phones AS (
SELECT NULLIF(@p_customer_business_phone, '') AS phone_no UNION ALL
SELECT NULLIF(@p_customer_cell_phone, '') UNION ALL
SELECT NULLIF(@p_customer_home_phone, '') UNION ALL
SELECT NULLIF(@p_customer_main_phone, '')
), emails AS (
SELECT NULLIF(@p_customer_personal_email, '') AS email_address UNION ALL
SELECT NULLIF(@p_customer_work_email, '') UNION ALL
SELECT NULLIF(@p_customer_main_email, '')
)
SELECT l.*
FROM Spels.Lead l
JOIN Spels.Vehicle v
ON l.lead_id = v.lead_id
AND l.hyperion_code = @p_hyperion
AND (
(v.vin = @p_vin) OR
(v.year = @p_year AND v.make = @p_make AND v.model = @p_model)
)
AND l.active_flg = 1
JOIN (
SELECT c.customer_id, c.last_name
FROM Spels.Customer c
JOIN Spels.Customer_Email ce ON c.customer_id = ce.customer_id
JOIN emails e ON ce.email_address = e.email_address
WHERE c.last_name = COALESCE(NULLIF(@p_customer_last_name , ''), c.last_name)
UNION ALL
SELECT c.customer_id, c.last_name
FROM Spels.Customer c
JOIN Spels.Customer_Phone cp ON c.customer_id = cp.customer_id
JOIN phones p ON Spels.fn_PhoneNumber(p.phone_no) = Spels.fn_PhoneNumber(cp.phone_no)
WHERE c.last_name = COALESCE(NULLIF(@p_customer_last_name , ''), c.last_name)
) contact ON l.customer_id = contact.customer_id;
```
### **Key Fixes**
- Used **session variables** with `@` prefix.
- Ensured **NULL handling** using `NULLIF()` and `COALESCE()`.
- Used `UNION ALL` instead of `UNION` (avoids unnecessary sorting).
- Verified `fn_PhoneNumber()` is handling `NULL` values properly.
- Checked AWS Aurora **MySQL version compatibility** (CTEs need MySQL 8.0+).
If you are using **Aurora MySQL 5.7**, you???ll need to replace `WITH` CTEs with **temporary tables** or **subqueries**.
Let me know if you need a version without CTEs! ????