Lecture presented at IIUG 2016, mainly directed to developers, but with several good practices from user cases, and suggested solutions and approaches.
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Don't make your Informix static - developers do's and don'ts tips
1. Alexandre Marini
Senior IT Consultant
Brazil
Don't make your Informix static -
developers do's and don'ts tips
2. Don't make your Informix static - developers do's and don'ts
tips
- Simple code changes can make a huge
impact
- Some “don'ts” specific cases
- How to use UDFs in SQL
- Tips for generic development / work
Agenda
3. Don't make your Informix static - developers do's and don'ts
tips
Simple code changes can make a huge impact
1) Choose a right data type/validation method
Create table [tabname]
(col1 integer,
col2 char(14),
col3 varchar(50),
col4 char(14),
primary key (cod1)
);
Columns 2 and 4 stores two kinds of records: personal or company ID,
depending on char length.
4. Don't make your Informix static - developers do's and don'ts
tips
Simple code changes can make a huge impact
- Validation UDR was running in procedure – isnumeric() based
- Going through all 14/11 digits for != [0-9]
- Running into a big table (around 320 K rows)
- 2 columns (cols 2 and 4)
- Data loading procedure: obviously as fast as a turtle!
- Simple line of cast turned the process 89% faster
ge: Col2::decimal(14,0)
5. Don't make your Informix static - developers do's and don'ts
tips
Simple code changes can make a huge impact
2) Verify your search patterns
Example: An ITIN, or Individual Taxpayer Identification Number, 9-digit
length, beginning with the number "9", formatted like an SSN (NNN-
NN-NNNN).
- searches with like %chars%, or matches *chars*, in an alpha
column with a pattern on the first “n” digits
select count(nmr_lancto)
from recconta
where cdg_fornecedor like "%01838723%" and dat_cancel is null
and nmr_or is null and dat_recebto is null
6. Don't make your Informix static - developers do's and don'ts
tips
Simple code changes can make a huge impact
- using like '%[digits]%', the cost is very high
- note that used indexes are the same: before and after changes
Estimated Cost: 13476
Estimated # of Rows Returned: 1
1) informix.recconta: INDEX PATH
(1) Index Name: informix.xie12recconta
Index Keys: dat_cancel nmr_or dat_recebto cdg_fornecedor (Key-First) (Serial,
fragments: ALL)
Lower Index Filter: ((informix.recconta.dat_recebto IS NULL AND
informix.recconta.nmr_or IS NULL ) AND informix.recconta.dat_cancel IS NULL )
Index Key Filters: (informix.recconta.cdg_fornecedor LIKE '%01838723%' )
7. Don't make your Informix static - developers do's and don'ts
tips
Simple code changes can make a huge impact
- rewritten query, just with like '[digits]%', (same begin
pattern)…
select count(nmr_lancto)
from recconta
where cdg_fornecedor like "01838723%" and
dat_cancel is null and nmr_or is null and dat_recebto is null
Estimated Cost: 79
Estimated # of Rows Returned: 1
1) informix.recconta: INDEX PATH
(1) Index Name: informix.xie12recconta
Index Keys: dat_cancel nmr_or dat_recebto cdg_fornecedor (Serial, fragments: ALL)
Lower Index Filter: (((informix.recconta.cdg_fornecedor LIKE '01838723%' AND
informix.recconta.dat_recebto IS NULL ) AND informix.recconta.nmr_or IS NULL ) AND
informix.recconta.dat_cancel IS NULL
8. Don't make your Informix static - developers do's and don'ts
tips
Simple code changes can make a huge impact
3) Be careful with trigger events – audit trigger example
ORIGINAL CODE:
CREATE TRIGGER [trigger_name] UPDATE on [table_name]
REFERENCING OLD AS o NEW AS n
for each row
if o.column1 != n.column2 then
insert into tbaudit_upd(values…)
end if
end;
RECOMMENDED CODE:
CREATE TRIGGER [trigger_name] UPDATE OF [audit_field] on [table_name]
REFERENCING OLD as o NEW AS n
for each row
WHEN n.column1 != o.column1
insert into tbaudit_upd(values….);
End;
- Focus on the most specific trigger event
Benefits: lower processing avoiding unecessary executions!
9. Don't make your Informix static - developers do's and don'ts
tips
Some don'ts specific cases
- Avoid DISTINCT, unless extremely needed:
Prefer cleaning and normalizing data, create constraints at the fields,
before insert triggers…
- Avoid *s:
- define clauses: define abc record like [table].*
- selects, deletes, into clauses
- updates: update [table] set [table.]* = record.*
- inserts clauses without column names
insert into [table] values (record.*)
A simple schema change, with one added column, will demand lots of
code changing! Time is money!
10. Don't make your Informix static - developers do's and don'ts
tips
- Avoid using UDFs in WHERE sections
SELECT 'Email sent: ' || email_subject || ' from ' ||
UPPER(email_from) || ' to ' || UPPER(email_to) || ' at ' ||
email_datetime
FROM USU_EMAIL
WHERE UPPER(email_from) =
'ALEXANDRE@MCSOFTWARE.COM.BR'
Estimated Cost: 326
Estimated # of Rows Returned: 500
1) usu_email: SEQUENTIAL SCAN
Filters: UPPER(usu_email.email_from ) =
'ALEXANDRE@MCSOFTWARE.COM.BR
How to use UDFs in SQL
11. Don't make your Informix static - developers do's and don'ts
tips
How to use UDFs in SQL
- Two possibilities:
1) Normalize data before input
2) Create a functional index
CREATE FUNCTION toUpper(name VARCHAR(100))
RETURNS VARCHAR(100) WITH (NOT
VARIANT);
RETURN upper( name );
END FUNCTION;
CREATE INDEX idx_upper_email ON USU_EMAIL
( toUpper(email_from) );
12. Don't make your Informix static - developers do's and don'ts
tips
How to use UDFs in SQL
- Running the same query again:
SELECT 'Email sent: ' || email_subject || ' from ' ||
UPPER(email_from) || ' to ' || UPPER(email_to) || ' at ' || email_datetime
FROM USU_EMAIL
WHERE UPPER(email_from) = `ALEXANDRE@MCSOFTWARE.COM.BR'
Estimated Cost: 2
Estimated # of Rows Returned: 2
1) usu_email: INDEX PATH
(1) Index Name: idx_upper_email
Index Keys: toupper(email_de) (Serial, fragments: ALL)
Lower Index Filter: toupper(usu_email.email_de)=
`ALEXANDRE@MCSOFTWARE.COM.BR'
UDRs are shown in sqexplain output:
UDRs in query:
--------------
UDR id : 454
UDR name: toupper
13. Don't make your Informix static - developers do's and don'ts
tips
Tips for generic development / work
- Simplifying (KISS), Reuse, Documenting
- Don't try to reinvent the wheel. Database
manager systems have UDRs to do whatever you
need
- Work together with your DBA. He must be
boring, but he can save your time, brains, and
provide you several tech tips
14. Don't make your Informix static - developers do's and don'ts
tips
Questions?
alexandre@mcsoftware.com.br