DB에서 Index를 만들자
DB는 potato를 찾아달라고 해서 기본적으로 a부터 찾기 시작함. 이게 기본 동작임.
하지만 index를 만들면 I부터 시작하는 식.
기본적으로 MsSQL DB는 relation이 생길 때마다 index 자동 생성해줌.

그러나 pscale은 이렇게 동작하지 않고, index를 생성하지 않기 때문에
User의 모든 Product를 보여달라고 하면
pscale은 DB에서 Product테이블 가서 모든 product를 뒤져서 해당 USER의 id를 가진 product를 골라온다.
너무 느리겠지. 그리고 테이블 읽을 때마다 돈 내기 때문에 비용문제도 생김.
다시말하지만 MySQL은 이런 인덱스를 알아서 생성한다.
인덱스를 통해 DB에 어떤 상품이 있는지 알 수있다.
그래서 새 상품 만들면 DB에 새 상품 데이터가 추가될거고, 추가로 index도 업데이트가 된다.
인덱스는 DB와 분리된 것이라 보면 됨.
그래서 id가 1인 유저가 상품을 등록하면 상품이 DB에 추가되고, 인덱스도 업데이트 된다.
그래서 아이디가 1인 유저의 상품을 조회하면 시간이 오래 걸리는 DB를 검색하는 대신
바로 인덱스를 조회한다.


아래 정리 잘 돼있네.
https://spiderwebcoding.tistory.com/6

아무튼 이렇게 바꾸고, 해당 브랜치에 연결 된 상태에서 기존과 똑같이 아래 명령어를 내리면 된다.

npx prisma db push

그리고 deploy request를 보내자.

깃헙처럼 변경된게 보여진다.

메인 브랜치가 업데이트 된걸로보아 merge가 된거다.
그래서 indexes 브랜치를 지울 수 있음. 역할이 끝났거든.
근데 그냥 dev브랜치 따서 지우지말고 계속 푸시하면되겠찌.

planet scale에서 Database를 dev와 prod로 나누어보자.
prod 브랜치로 promote하면 아래 3가지 좋은 점이 있다.
1, 스키마 직접수정 불가
2. 가용성 증대
3. 자동백업

그리고 브랜치를 만들고 연결해보자.

pscale connect carrot-market indexes
//indexes는 새 브랜치 이름

To update your main branch, you can make schema changes to this branch. Once you're ready, open a deploy request with your changes.

브랜치 바뀌면 데이터도 사라짐. 같은 스키마를 갖고 있지만 data는 다르다.

12.0 Models
포스트와 그 포스트에 대한 궁금증과, 답변, 궁금해요 모델 추가.

model Post {
  id         Int         @id @default(autoincrement())
  created    DateTime    @default(now())
  updated    DateTime    @updatedAt
  user       User        @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId     Int
  question   String      @db.MediumText
  answers    Answer[]
  wonderings Wondering[]

  @@index([userId])
}

model Answer {
  id      Int      @id @default(autoincrement())
  created DateTime @default(now())
  updated DateTime @updatedAt
  user    User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId  Int
  post    Post     @relation(fields: [postId], references: [id], onDelete: Cascade)
  postId  Int
  answer  String   @db.MediumText

  @@index([userId])
  @@index([postId])
}

model Wondering {
  id      Int      @id @default(autoincrement())
  created DateTime @default(now())
  updated DateTime @updatedAt
  user    User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId  Int
  post    Post     @relation(fields: [postId], references: [id], onDelete: Cascade)
  postId  Int

  @@index([userId])
  @@index([postId])
}

 

12.1 Forms and Handlers
12.2 Post Detail
12.3 궁금해요
12.4 Answer
12.5 All Posts

12.6 useCoords
12.7 Geo Search
12.8 Geo Bug


const posts = await client.post.findMany({
        include: {
          user: {
            select: {
              id: true,
              name: true,
              avatar: true,
            },
          },
          _count: {
            select: {
              wonderings: true,
              answers: true,
            },
          },
        },
        where: {
          latitude: { gte: parsedLatitude - 0.01, lte: parsedLatitude + 0.01 },
          longitude: {
            gte: parsedLongitude - 0.01,
            lte: parsedLongitude + 0.01,
          },
        },
      });

 

Type 'number' is not assignable to type 'null'.ts(2322)
해결 null as any로 바꾸면 된다는데

 
next.js에서는 페이지를 미리 만들어 놓기 때문에, 초기값으로 그려진다.

그래서 longitude, latitude가 null이었으니 제대로 동작 안하지. 

 

'클론코딩-캐럿마켓 > 전반' 카테고리의 다른 글

14 Streams - seeding  (0) 2023.01.11
13 Profile  (0) 2023.01.09
11 Products  (0) 2023.01.04
10 AUTHORIZATION - SWR  (0) 2023.01.04
9 AUTHENTICATION  (0) 2022.12.27

+ Recent posts