📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Inner and Outer Joins

2022-08-01 •Youtube

Detailed Notes

1. inner joins vs outer joins

insert into stats(code,value1,value2) values ('TEST',6,12345);

select s.code,l.code from stats s inner join lkp_type l on l.type_id=s.value1;

select s.code,addr.city,addr.state,addr.zip from stats s inner join address addr on addr.t_one_id=s.value1;

select s.code,addr.city,addr.state,addr.zip from stats s left join address addr on addr.t_one_id=s.value1;

select s.code,addr.city,addr.state,addr.zip from stats s left join address addr on addr.t_one_id=s.value1 where addr.city is null;

select s.code,addr.city,addr.state,addr.zip,l.code from stats s left join address addr on addr.t_one_id=s.value1 left join lkp_type l on l.type_id=addr.type_id;

select s.code from stats s where s.value1 not in (select type_id from lkp_type);

select s.code from stats s inner join (select type_id from lkp_type where code='HOME') src on src.type_id=s.value1;

select s.code,addr.city,addr.state,addr.zip from stats s left join address addr on addr.t_one_id=s.value1 where a;

select s.code,l.code from stats s left join lkp_type l on l.type_id=s.value1 where l.code='HOME' or l.code is null;

Transcript Text
[Music]
well hello and welcome back we are
continuing our my sequel series we've
taken a little bit of time off here took
a break
but now we're diving back in
and this episode i want to look at
indexes uh we've talked
uh we've not really talked too much
about them
i would just sort of mention them in
passing
and so for this case
i have a new
uh table that i've created
and there will be a sequel out there
because it has a lot of rows
now if we go back to let's go show our
tables
and doing so
we can see we've got you know sort of
our traditional tables actually let me
do this
been so long i forgot my other id but
that's okay
okay so now you can see our tables and
there's a new one here
called horses
now if you remember
let me see actually i'm just gonna do an
id
from horses i'm gonna see how many rows
i've got to show you that okay we got
350 000 rows
and so it takes a second
uh actually 0.109 seconds to make that
call
whoops
i want to do a little brief thing there
we go
or once it's in there we can just do a
count star now if we look at horses it's
got a bunch of different
columns
now right now
we have on the id
we do have a primary key set up and we
do have on name we do have an index
which is this we have a multi multi-key
index set up on name
so we'll talk we'll look at those a
little bit first
uh how do you create an index
well you're going to create an index
by
using the phrase
using create index and then you're going
to give an index name what table and
then what column
so if i wanted to
and let's start with this um
let's do
let's work we're gonna do work from
damn and sire so they're mother and
father
so let's do
we'll just do this one this may take a
second
let's do this like distinct damn
from
horses
uh and we're gonna move it to like 100.
oh
goes that way so let's do
let's look at super mess
now for these today we're going to look
this little part here particularly that
thing in parentheses is going to be key
because this is essentially how fast it
ran now there are
other ways to get into this but just to
show you from a high level point of view
the effect of indexes
or indices
then we're going to take a look at that
so now
let's do slick star from
horses where damn equals
super mess so
there we go so i only got two horses it
took us
0.413 seconds
that's
a little bit of time you know that's
almost half a second
to figure this one out and this is for
one row
so
let's see what happens when we create an
index let's remember that's create index
and we're going to give it a name so
let's call this uh damn name
actually we're going to use it horse's
damn name
on
horses
now typically what you want to do you
depends on where you're at but for the
most part your index name should be
in unique even though when we disable
them when we delete them
we're gonna we'll see that we use a
table name
but
it helps do that it's just easier when
you're doing debugging because
you'll be able to see stuff where
particularly when you you look into
performance tuning and such you'll be
able to see which index is being used
specifically
so if i create an index it didn't take
too long in this case sometimes it can
take
a while to build an index depending on
how big the table is
but you see here it took us a little
over half a second so now
let's go back to that prior
call that we made
and now it's coming back in .01 seconds
it's coming back instantly basically
because
it's using this index so now if we do
the same thing with
sire where does that
get the distinct sire
and then oh actually let's just go back
to that one um
there we go so here the sire is spinster
so now if we use that
so we're going to look at we're going to
get that same row
where sire equals
and now granted uh took a little bit so
let's do
let's find some more about that
uh let's say
ambreeder id equals two one so we're
gonna
add a little bit more
and
breeder equals
two eight one nine
oh breeder id my mistake
so now we're back to that 0.41 seconds
now if we
did the same thing and actually let's
sort we'll keep some of these
well you'll allow you guys to do your
almost uh selects
um
so now if we do the same thing and
we do damn equals
super mess
now we get it back super fast no pun
intended because it's going to pick up
because that name's in there
it this is where
this
my sql engine is smart now
most modern databases are like this
some are not if you go back to older
versions
it only
it basically just looks for an index
here and then stops when it doesn't find
it or will look for a combo index
in which case you're going to find
something that's going to be
it's going to take a little bit longer
now
let's do
to show a multi-column index so let's go
back to
here oh
uh so we're here's where we have that
breeder id
now it's coming back pretty quick
because it's cached right now
because we've already seen this exact
query
so if we do it with
uh and we've already done this one so
let's change up
uh let's do breeder id here and let's
see if we can get a different sire
oh he's only got one uh let's see if we
get a just go off by one see there we go
so we've got something a little
different now it takes a little bit
to find this one so now what we're gonna
do is we're gonna do this one
where sire equals jess's dream
and we're going to see it's going to
take
oh
shoot
my mistake because it doesn't like that
i'm going to have to
let's just change oh let's change that
whole thing up
oh sorry
tutorial
and then
let's go back and let's pick uh
65 let's see if that gives us something
nope
64.
oh
63.
oh come on give me a breeder id
62. there we go okay so there we got one
and this is going to be
so now if we do
well let's do a couple because
i want to see
well let's do where
uh
let's go off of
well let's see
i think i have my name
already in there so if i do our name
equals
strong
eagle
yes i already have a name index in there
it's gonna be hard to show this but
let's go in there and create a
multi-column index so let's say well
let's say that we want to see
now we already have uh dam in there but
what we could do is let's say
we want to find a breeder
what would this be that would be an
owner breeder combo
let's say we're going to search on that
a lot so in that case it's pretty easy
to do we're going to do create index
uh horse
breeder
owner let me call it whatever you want
basically
on horses
and then just give it a list of the ids
so we're gonna do owner id comma breeder
id
it took us a little over half a second
and so now if i do select star from
horses
where
owner id equals one two six
four four eight
and breeder id
equals 28162
it's going to come back pretty much
instantly boom so it's going to
use both of those
now if i do
let's say i go back to owner id so
that's i've got to have those combos so
now if i go pick let's just pick a
there
it still came back pretty quick
because it's probably looking at that
without the breeder id this is where
mike staples well the database engines
in general are getting sort of smart so
if there's an index that's useful
then it's going to be able to do it so
now i'm also probably going to see if i
come back with breeder id of
oh let's just pick some number let's see
if it comes back
uh it's an empty set it did take a while
because it's breeder id but if i did
owner id
oh
i do owner id
equals and let's just do an empty set
again so let's do um
this guy
and reader id oh shoot reader id
inverter id equals this uh
yeah
it comes back pretty quick because it's
hitting the
here
it's not hitting a
index at all
here it's trying to hit an index is
saying i don't have that so that
disappears pretty quick
so same thing if i do where owner id and
i don't give it
here because i just uh so that was just
in there so let's give it like a
different number
whoop
well let's try that that's going to come
back too quick because it's too out of
the range
it's probably going to come back super
fast now anyways oh there it was but
it's still it's hitting because you're
looking at index
uh and it's going to i guess because
it's because of the first column it's
going to pick that up this is where
how a database uses
an index can be
um
it varies from database and it can take
a little bit of work to figure out what
index is being using used and how it's
being used
so for example you may now you don't
want to overly index something but you
could have an index on in this case
breeder but also an index on breeder
owner
you probably don't really need that
but there are some cases where like for
example would only use one of those if
that's the
the id in there now we have one for
name and breeder
so we could come in
and let's see what that how that works
so if we do name equals and it's
probably going to find whichever is the
fastest so
name is turalura
and let's say owner id
equals oh we did
oh and damn that's what we had before
and damn equals
so these are both
we have indexes for both of these
and it's going to come back pretty quick
but you don't really know in this case
which one it's hitting
now also just to show how these things
have changed we can see here
that name has an index
dam has an index and owner has an index
and that's what that key is
then there's some other things you can
do i think we've we've talked about like
you can make them unique and things of
that nature but
even here now granted
you know if you've got 10 rows in
database which is why i grabbed this one
i just threw this thing in there
because i wanted something that had more
than you know a few rows because you're
not going to see those time differences
but this you know we were seeing half a
second versus a tenth you know or what
is that a thousandth of a second
differences which is
almost instantaneous versus half a sec
or almost half a second with 300 000
rows
now if you start to move up with
a million rows or tens of millions of
rows and then start joining tables based
on that
it can take quite a while
so if i were to
let's say create
table
owners
as select
all righty come up
let's just get something here
come a sire
from horses
it's going to take a second and so now
i've got 300 000 in rows rows there so
now if i do select
star from
uh
horses
uh let's just do this
let's do
owners dot
sire
from horses
enter join
owners on
courses dot
uh owner id
equals
owners dot owner id
where
uh let's see owner id equal well let's
say where
sire equals
idiot proof i think is one
we're gonna see
uh oh that's ambiguous so let's do based
on owners
that sire
okay so it took ah it wasn't too bad it
came back moderately quickly
um
if i do well probably this is going to
be super fast now if i do horses.sire
oh well there you go
oh because owners have nothing to do
with that so okay so
uh this again it's taking a little bit
because
we're joining on this id that does have
an index in
horses
right yeah it does have an index in
horses but it doesn't on
the other table so if we take the same
thing and let's create
um can i do that real quick so if we
create table breeders
the same way
as select breeder id from horses
now we don't have
any index here on either table
and then we come in and we do
select
breeders.sire
from horses
or even let's say from breeders from
breeder
enter join
horses on
horses dive reader
id equals breeders
dot reader
id
uh what did i do
oh from
that's
all the way back here it's not form it
should be from
there we go that's gonna take us now
look it's taken a while
because we don't have any indexes set up
so now since we're doing this search
across
a lot of records
300 000 well 350 000 times 350 000
it's gonna take it a little bit
and that puts us basically the end of
this lesson because this thing will not
finish i bet before i do
so
what we've covered today is indexes and
we're going to do some more playing
around with stuff and next time around
but until then go out there and have
yourself a great day a great week and we
will talk to you
next time
yep it is still
running
you
Transcript Segments
0.43

[Music]

27.039

well hello and welcome back we are

29.359

continuing our my sequel series we've

32.239

taken a little bit of time off here took

33.6

a break

34.559

but now we're diving back in

36.96

and this episode i want to look at

39.76

indexes uh we've talked

42.239

uh we've not really talked too much

43.84

about them

45.6

i would just sort of mention them in

47.12

passing

48.239

and so for this case

50.64

i have a new

52

uh table that i've created

54.16

and there will be a sequel out there

56.48

because it has a lot of rows

59.039

now if we go back to let's go show our

61.199

tables

64.64

and doing so

66.4

we can see we've got you know sort of

68

our traditional tables actually let me

69.76

do this

72

been so long i forgot my other id but

74.96

that's okay

77.92

okay so now you can see our tables and

80

there's a new one here

82.08

called horses

83.92

now if you remember

88.56

let me see actually i'm just gonna do an

90.24

id

92

from horses i'm gonna see how many rows

93.6

i've got to show you that okay we got

95.52

350 000 rows

98.799

and so it takes a second

101.52

uh actually 0.109 seconds to make that

104.88

call

105.68

whoops

107.2

i want to do a little brief thing there

108.799

we go

109.759

or once it's in there we can just do a

112.079

count star now if we look at horses it's

114.32

got a bunch of different

116.479

columns

119.36

now right now

121.68

we have on the id

124.32

we do have a primary key set up and we

126.799

do have on name we do have an index

131.039

which is this we have a multi multi-key

133.36

index set up on name

135.84

so we'll talk we'll look at those a

137.12

little bit first

138.56

uh how do you create an index

140.8

well you're going to create an index

142.4

by

144.239

using the phrase

145.76

using create index and then you're going

147.52

to give an index name what table and

149.52

then what column

151.12

so if i wanted to

154.16

and let's start with this um

157.84

let's do

162.319

let's work we're gonna do work from

165.12

damn and sire so they're mother and

167.92

father

169.12

so let's do

170.959

we'll just do this one this may take a

172.48

second

174.8

let's do this like distinct damn

179.28

from

180.319

horses

183.36

uh and we're gonna move it to like 100.

190.72

oh

193.519

goes that way so let's do

196.159

let's look at super mess

200.319

now for these today we're going to look

202.08

this little part here particularly that

203.84

thing in parentheses is going to be key

205.519

because this is essentially how fast it

207.599

ran now there are

209.28

other ways to get into this but just to

211.76

show you from a high level point of view

214.239

the effect of indexes

216.64

or indices

218.159

then we're going to take a look at that

219.84

so now

221.44

let's do slick star from

226.959

horses where damn equals

230.799

super mess so

232.799

there we go so i only got two horses it

234.799

took us

236.36

0.413 seconds

238.48

that's

239.68

a little bit of time you know that's

241.84

almost half a second

243.599

to figure this one out and this is for

245.84

one row

247.439

so

248.239

let's see what happens when we create an

249.84

index let's remember that's create index

253.519

and we're going to give it a name so

255.36

let's call this uh damn name

259.759

actually we're going to use it horse's

261.519

damn name

266.08

on

267.199

horses

271.68

now typically what you want to do you

273.84

depends on where you're at but for the

275.44

most part your index name should be

279.04

in unique even though when we disable

281.52

them when we delete them

283.36

we're gonna we'll see that we use a

285.199

table name

286.639

but

288.16

it helps do that it's just easier when

289.84

you're doing debugging because

292.08

you'll be able to see stuff where

294.639

particularly when you you look into

296.88

performance tuning and such you'll be

298.4

able to see which index is being used

300.32

specifically

301.84

so if i create an index it didn't take

303.28

too long in this case sometimes it can

305.52

take

306.32

a while to build an index depending on

308.72

how big the table is

310.56

but you see here it took us a little

311.759

over half a second so now

313.68

let's go back to that prior

317.68

call that we made

320

and now it's coming back in .01 seconds

323.52

it's coming back instantly basically

326.08

because

327.28

it's using this index so now if we do

329.759

the same thing with

331.84

sire where does that

334.56

get the distinct sire

340.72

and then oh actually let's just go back

342.56

to that one um

345.199

there we go so here the sire is spinster

351.68

so now if we use that

354.24

so we're going to look at we're going to

355.199

get that same row

358.24

where sire equals

360.479

and now granted uh took a little bit so

362.479

let's do

364.72

let's find some more about that

367.36

uh let's say

369.44

ambreeder id equals two one so we're

371.68

gonna

372.4

add a little bit more

374.72

and

375.919

breeder equals

377.68

two eight one nine

383.12

oh breeder id my mistake

388.24

so now we're back to that 0.41 seconds

391.44

now if we

392.84

did the same thing and actually let's

395.52

sort we'll keep some of these

398.96

well you'll allow you guys to do your

400.96

almost uh selects

403.68

um

404.96

so now if we do the same thing and

410

we do damn equals

413.599

super mess

419.84

now we get it back super fast no pun

422.72

intended because it's going to pick up

425.199

because that name's in there

427.68

it this is where

429.199

this

430

my sql engine is smart now

432.479

most modern databases are like this

435.44

some are not if you go back to older

437.199

versions

438.319

it only

439.919

it basically just looks for an index

442.319

here and then stops when it doesn't find

444.72

it or will look for a combo index

449.039

in which case you're going to find

450.4

something that's going to be

452.56

it's going to take a little bit longer

455.12

now

456.4

let's do

458.16

to show a multi-column index so let's go

460.8

back to

464.319

here oh

465.919

uh so we're here's where we have that

467.52

breeder id

468.96

now it's coming back pretty quick

470.24

because it's cached right now

472.8

because we've already seen this exact

476.56

query

478.879

so if we do it with

481.039

uh and we've already done this one so

482.479

let's change up

486.08

uh let's do breeder id here and let's

488.56

see if we can get a different sire

494.4

oh he's only got one uh let's see if we

496.639

get a just go off by one see there we go

499.44

so we've got something a little

500.16

different now it takes a little bit

501.84

to find this one so now what we're gonna

504.16

do is we're gonna do this one

508.16

where sire equals jess's dream

521.519

and we're going to see it's going to

522.64

take

524.64

oh

526.16

shoot

528.8

my mistake because it doesn't like that

530.959

i'm going to have to

535.2

let's just change oh let's change that

537.12

whole thing up

540.72

oh sorry

542.399

tutorial

545.04

and then

546.16

let's go back and let's pick uh

548.48

65 let's see if that gives us something

550.24

nope

551.04

64.

553.2

oh

554.72

63.

556.16

oh come on give me a breeder id

559.12

62. there we go okay so there we got one

563.04

and this is going to be

568

so now if we do

572

well let's do a couple because

574.48

i want to see

580.8

well let's do where

586

uh

587.839

let's go off of

591.68

well let's see

594.32

i think i have my name

597.279

already in there so if i do our name

599.44

equals

601.839

strong

602.839

eagle

604.8

yes i already have a name index in there

607.6

it's gonna be hard to show this but

610.399

let's go in there and create a

612.399

multi-column index so let's say well

614.24

let's say that we want to see

617.2

now we already have uh dam in there but

620.72

what we could do is let's say

622.64

we want to find a breeder

625.519

what would this be that would be an

626.959

owner breeder combo

629.519

let's say we're going to search on that

630.959

a lot so in that case it's pretty easy

633.279

to do we're going to do create index

635.76

uh horse

637.839

breeder

639.6

owner let me call it whatever you want

641.279

basically

642.48

on horses

644.24

and then just give it a list of the ids

645.76

so we're gonna do owner id comma breeder

649.92

id

654.399

it took us a little over half a second

656.48

and so now if i do select star from

658.8

horses

660.959

where

663.519

owner id equals one two six

667.839

four four eight

669.92

and breeder id

672.959

equals 28162

675.68

it's going to come back pretty much

676.959

instantly boom so it's going to

679.76

use both of those

682.56

now if i do

684.88

let's say i go back to owner id so

687.04

that's i've got to have those combos so

689.04

now if i go pick let's just pick a

693.68

there

694.56

it still came back pretty quick

696.959

because it's probably looking at that

698.959

without the breeder id this is where

701.279

mike staples well the database engines

703.519

in general are getting sort of smart so

706.24

if there's an index that's useful

708.48

then it's going to be able to do it so

710.16

now i'm also probably going to see if i

711.519

come back with breeder id of

714.24

oh let's just pick some number let's see

717.76

if it comes back

720.079

uh it's an empty set it did take a while

722.16

because it's breeder id but if i did

724.8

owner id

727.04

oh

729.36

i do owner id

731.2

equals and let's just do an empty set

733.2

again so let's do um

735.839

this guy

737.68

and reader id oh shoot reader id

743.68

inverter id equals this uh

747.44

yeah

749.279

it comes back pretty quick because it's

751.44

hitting the

753.76

here

754.56

it's not hitting a

756.24

index at all

758.399

here it's trying to hit an index is

759.92

saying i don't have that so that

761.279

disappears pretty quick

762.959

so same thing if i do where owner id and

765.68

i don't give it

770.16

here because i just uh so that was just

772

in there so let's give it like a

772.959

different number

774.639

whoop

775.519

well let's try that that's going to come

777.2

back too quick because it's too out of

778.8

the range

781.12

it's probably going to come back super

782.399

fast now anyways oh there it was but

784.079

it's still it's hitting because you're

785.6

looking at index

787.839

uh and it's going to i guess because

789.6

it's because of the first column it's

791.2

going to pick that up this is where

795.04

how a database uses

798

an index can be

800.959

um

802.48

it varies from database and it can take

804.24

a little bit of work to figure out what

806.079

index is being using used and how it's

808.24

being used

809.68

so for example you may now you don't

812.16

want to overly index something but you

814

could have an index on in this case

816.32

breeder but also an index on breeder

818.959

owner

820.8

you probably don't really need that

824

but there are some cases where like for

826.48

example would only use one of those if

829.04

that's the

830.959

the id in there now we have one for

833.839

name and breeder

835.68

so we could come in

837.279

and let's see what that how that works

838.639

so if we do name equals and it's

840.32

probably going to find whichever is the

841.519

fastest so

843.6

name is turalura

848.24

and let's say owner id

851.279

equals oh we did

854.88

oh and damn that's what we had before

857.12

and damn equals

858.88

so these are both

863.839

we have indexes for both of these

866.16

and it's going to come back pretty quick

867.44

but you don't really know in this case

869.36

which one it's hitting

871.839

now also just to show how these things

874.399

have changed we can see here

877.839

that name has an index

880.56

dam has an index and owner has an index

883.36

and that's what that key is

885.199

then there's some other things you can

886.399

do i think we've we've talked about like

888.399

you can make them unique and things of

890

that nature but

892.32

even here now granted

894.88

you know if you've got 10 rows in

896.959

database which is why i grabbed this one

899.839

i just threw this thing in there

902.399

because i wanted something that had more

903.92

than you know a few rows because you're

905.36

not going to see those time differences

907.839

but this you know we were seeing half a

910

second versus a tenth you know or what

912

is that a thousandth of a second

914.72

differences which is

916.88

almost instantaneous versus half a sec

919.44

or almost half a second with 300 000

921.68

rows

922.56

now if you start to move up with

926.48

a million rows or tens of millions of

929.199

rows and then start joining tables based

932.8

on that

933.92

it can take quite a while

937.44

so if i were to

939.44

let's say create

941.839

table

945.279

owners

947.759

as select

955.12

all righty come up

958.48

let's just get something here

961.04

come a sire

964.56

from horses

968.56

it's going to take a second and so now

970.079

i've got 300 000 in rows rows there so

972.639

now if i do select

974.48

star from

978.48

uh

979.279

horses

982.88

uh let's just do this

985.04

let's do

986.079

owners dot

989.04

sire

992.56

from horses

994.639

enter join

996.56

owners on

1000.48

courses dot

1004.48

uh owner id

1006.959

equals

1008.839

owners dot owner id

1016.8

where

1019.519

uh let's see owner id equal well let's

1023.36

say where

1025.36

sire equals

1029.039

idiot proof i think is one

1032.959

we're gonna see

1036.4

uh oh that's ambiguous so let's do based

1039.6

on owners

1042.48

that sire

1044.4

okay so it took ah it wasn't too bad it

1047.12

came back moderately quickly

1049.44

um

1051.28

if i do well probably this is going to

1052.96

be super fast now if i do horses.sire

1059.28

oh well there you go

1065.679

oh because owners have nothing to do

1067.6

with that so okay so

1070.48

uh this again it's taking a little bit

1073.52

because

1074.64

we're joining on this id that does have

1077.28

an index in

1081.84

horses

1083.44

right yeah it does have an index in

1085.12

horses but it doesn't on

1087.2

the other table so if we take the same

1088.88

thing and let's create

1093.039

um can i do that real quick so if we

1095.12

create table breeders

1098.64

the same way

1101.679

as select breeder id from horses

1105.76

now we don't have

1107.919

any index here on either table

1112.799

and then we come in and we do

1116.24

select

1118.96

breeders.sire

1121.039

from horses

1124.24

or even let's say from breeders from

1126.4

breeder

1128.88

enter join

1130.4

horses on

1132.24

horses dive reader

1136.32

id equals breeders

1139.52

dot reader

1141.52

id

1145.44

uh what did i do

1152.08

oh from

1153.28

that's

1154.32

all the way back here it's not form it

1157.2

should be from

1160.64

there we go that's gonna take us now

1162.559

look it's taken a while

1164.72

because we don't have any indexes set up

1168.559

so now since we're doing this search

1170.799

across

1172.08

a lot of records

1173.679

300 000 well 350 000 times 350 000

1177.2

it's gonna take it a little bit

1179.6

and that puts us basically the end of

1181.84

this lesson because this thing will not

1183.84

finish i bet before i do

1186.16

so

1187.28

what we've covered today is indexes and

1189.76

we're going to do some more playing

1190.72

around with stuff and next time around

1193.12

but until then go out there and have

1194.799

yourself a great day a great week and we

1197.36

will talk to you

1199.2

next time

1200.4

yep it is still

1202.4

running

1219.12

you