Question

1) My question is that, when i have two large table which cannot be alter because of there sizes. Now i have to join them on a common field and now compare one field from both the table which is having same data but one's data type is int and anothers is varchar. I know we can done this easily, but when table have millions of record then comparing between two different data type is slow down, how can i make it fast.

2) my similar 2nd question is that when i have to join two tables on some field like id and which is in different data type in both the table. like one is int and another is char.....how can i join this two table because i cannot wait for many days.

(One solution i have tried is to create new table as an abstract(by in file out file) of old . While i have now changed the data type from char to int during create table and then took the in file)

If anybody have any other solution, please share

Was it helpful?

Solution

Make sure the conversion happens on the first table in the join, that way:

  1. the conversion only happens only once per row
  2. indexes can be used to join with the second table

for example:

select * 
from table1
join table2 on table2.intcol = cast(table1.varcharcol as signed)

This sample query will use an index on table2.intcol (if one exists) to join the two tables.

OTHER TIPS

Yes cast can be used for changing data types

select * 
from table1
join table2 on table2.intcol = cast(col as int)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top