>  기사  >  웹 프론트엔드  >  알아야 할 필수 JavaScript 인터뷰 질문

알아야 할 필수 JavaScript 인터뷰 질문

PHPz
PHPz원래의
2024-07-18 07:11:49718검색

Essential JavaScript Interview Questions You Need to Know

소개

CQL(Cypher Query Language)은 그래프 데이터베이스 쿼리를 위해 설계된 강력한 도구입니다. 기존 관계형 데이터베이스와 달리 그래프 데이터베이스는 정의되지 않은 관계로 밀접하게 연결된 데이터를 관리하는 데 탁월합니다. CQL은 직관적이고 강력한 구문을 제공하므로 그래프 데이터베이스에 저장된 데이터를 더 쉽게 생성, 읽기, 업데이트 및 삭제할 수 있습니다. 이 포괄적인 가이드에서는 CQL의 기능, 제약 조건, 용어 및 명령을 살펴보고 CQL의 잠재력을 최대한 활용하는 데 도움이 되는 실제 사례를 살펴보겠습니다.

목차

CQL(Cypher Query Language)의 특징

밀접하게 연결된 데이터에 적합

CQL의 뛰어난 기능 중 하나는 밀접하게 연결된 데이터에 대한 적합성입니다. 관계가 관리하기 복잡하고 번거로운 관계형 데이터베이스와 달리 그래프 데이터베이스는 연결을 통해 발전합니다. CQL은 이러한 관계를 직관적이고 효율적으로 쿼리할 수 있으므로 소셜 네트워크, 추천 엔진 등에 이상적인 선택입니다.

노드에 대한 다중 레이블

CQL에서는 노드가 여러 레이블과 연결될 수 있습니다. 이러한 유연성 덕분에 데이터를 더 효과적으로 구성하고 분류할 수 있습니다. 예를 들어, 사람을 나타내는 노드에는 개인 신원의 다양한 측면을 나타내는 개인, 직원, 고객과 같은 레이블이 있을 수 있습니다.

CQL의 제약

조각화 제한

CQL은 강력하지만 몇 가지 제약이 있습니다. 조각화는 특정 도메인에서만 가능합니다. 이는 어떤 경우에는 확실한 답변을 검색하기 위해 데이터 전체를 탐색해야 할 수도 있음을 의미합니다.

확실한 답변을 위한 전체 그래프 순회

일부 쿼리, 특히 복잡한 관계와 관련된 쿼리의 경우 반환된 데이터가 정확하고 완전한지 확인하기 위해 전체 그래프를 탐색해야 할 수도 있습니다. 이는 그래프의 크기와 복잡성에 따라 리소스 집약적이고 시간이 많이 걸릴 수 있습니다.

CQL의 용어

마디

노드는 그래프의 개체를 나타냅니다. 노드에는 이름, 나이 또는 기타 관련 속성과 같은 엔터티에 대한 정보를 저장하는 속성이 있을 수 있습니다.

상표

레이블을 사용하면 노드를 그룹화할 수 있습니다. 이는 SQL의 테이블 개념을 대체합니다. 예를 들어, Person이라는 레이블이 있는 노드는 사람을 나타내는 모든 노드를 그룹화합니다.

관계

관계는 두 노드 사이의 구체화된 링크입니다. 이는 SQL의 관계 개념을 대체하여 엔터티 간의 직접 연결을 가능하게 합니다.

속성

속성은 노드나 관계가 가질 수 있는 속성입니다. 예를 들어, Person 노드에는 이름, 나이와 같은 속성이 있을 수 있는 반면 LIKES 관계에는 Since와 같은 속성이 있을 수 있습니다.

CQL의 기본 명령

만들다

CREATE 명령은 노드와 관계를 생성하는 데 사용됩니다. 이는 그래프 구조를 구축하는 데 기본입니다.

성냥

MATCH 명령은 그래프에서 패턴을 검색하는 데 사용됩니다. CQL 쿼리의 초석으로, 지정된 기준에 따라 노드와 관계를 검색할 수 있습니다.

노드 생성

기본 노드 생성

CQL에서 노드를 만드는 것은 간단합니다. CREATE 명령을 사용하고 노드 세부정보를 입력하세요.

CREATE (:Person {name:\"John\", age:30})
CREATE (:Food {name:\"Pizza\"})

속성을 사용하여 노드 생성

노드에 대한 정보를 저장하는 키-값 쌍인 속성을 사용하여 노드를 생성할 수 있습니다.

CREATE (:Person {name:\"Jane\", age:25, occupation:\"Engineer\"})
CREATE (:Food {name:\"Burger\", calories:500})

노드 검색

기본 노드 검색

MATCH 명령을 사용하면 그래프에서 노드를 검색할 수 있습니다.

MATCH (p:Person) RETURN p

WHERE 절을 사용한 고급 검색

보다 구체적인 검색을 위해서는 WHERE 절을 사용하여 해당 속성을 기준으로 노드를 필터링하세요.

MATCH (p:Person)
WHERE p.age > 20
RETURN p.name, p.age

관계 만들기

노드를 생성하는 동안 관계 생성

노드를 생성하면서 노드 간의 관계도 생성할 수 있습니다.

CREATE (p:Person {name:\"John\", age:30})-[:LIKES]->(f:Food {name:\"Pizza\"})

기존 노드 간의 관계 생성

MATCH 명령을 사용하여 기존 노드 간에 관계를 생성할 수도 있습니다.

MATCH (p:Person {name:\"John\"})
MATCH (f:Food {name:\"Pizza\"})
CREATE (p)-[r:LIKES]->(f)
RETURN r

노드 및 관계 수정

속성 추가

SET 명령을 사용하여 기존 노드에 속성을 추가할 수 있습니다.

MATCH (p:Person {name:\"John\"})
SET p.occupation = \"Developer\"
RETURN p

속성 삭제

속성을 삭제하려면 해당 값을 NULL로 설정하세요.

MATCH (p:Person {name:\"John\"})
SET p.age = NULL
RETURN p

Modifying Attributes

Attributes can be modified by setting them to new values.

MATCH (p:Person {name:\"John\"})
SET p.age = 35
RETURN p

Using Aggregate Functions in CQL

COUNT

The COUNT function returns the number of nodes or relationships.

MATCH (n) RETURN count(n)

AVG

The AVG function calculates the average value of a numeric property.

MATCH (n) RETURN avg(n.age)

SUM

The SUM function calculates the total sum of a numeric property.

MATCH (n) RETURN sum(n.age)

Advanced Queries in CQL

Number of Relations by Type

To get the count of each type of relationship in the graph, use the type function.

MATCH ()-[r]->() RETURN type(r), count(*)

Collecting Values into Lists

The COLLECT function creates a list of all values for a given property.

MATCH (p:Product)-[:BELONGS_TO]->(o:Order)
RETURN id(o) as orderId, collect(p)

Database Maintenance in CQL

Deleting Nodes and Relationships

To delete all nodes and relationships, use the DELETE command.

MATCH (a)-[r]->(b) DELETE a, r, b

Visualizing Database Schema

Visualize the database schema to understand the structure of your graph.

CALL db.schema.visualization YIELD nodes, relationships

Practical Tricks and Tips

Finding Specific Nodes

Here are three ways to find a node representing a person named Lana Wachowski.

// Solution 1
MATCH (p:Person {name: \"Lana Wachowski\"})
RETURN p

// Solution 2
MATCH (p:Person)
WHERE p.name = \"Lana Wachowski\"
RETURN p

// Solution 3
MATCH (p:Person)
WHERE p.name =~ \".*Lana Wachowski.*\"
RETURN p

Complex Query Examples

Display the name and role of people born after 1960 who acted in movies released in the 1980s.

MATCH (p:Person)-[a:ACTED_IN]->(m:Movie)
WHERE p.born > 1960 AND m.released >= 1980 AND m.released < 1990
RETURN p.name, a.roles

Add the label Actor to people who have acted in at least one movie.

MATCH (p:Person)-[:ACTED_IN]->(:Movie)
WHERE NOT (p:Actor)
SET p:Actor

Application Examples

Real-World Use Cases

Consider a database for an online store where you need to manage products, clients, orders, and shipping addresses. Here's how you might model this in CQL.

Example Queries

Let's create some example nodes and relationships for an online store scenario:

CREATE (p1:Product {id: 1, name: \"Laptop\", price: 1000})
CREATE (p2:Product {id: 2, name: \"Phone\", price: 500})
CREATE (c:Client {id: 1, name: \"John Doe\"})
CREATE (o:Order {id: 1, date: \"2023-06-01\"})
CREATE (adr:Address {id: 1, street: \"123 Main St\", city: \"Anytown\", country: \"USA\"})

Now, let's create the relationships between these nodes:

CREATE (p1)-[:BELONGS_TO]->(o)
CREATE (p2)-[:BELONGS_TO]->(o)
CREATE (c)-[:MADE]->(o)
CREATE (o)-[:SHIPPED_TO]->(adr)

Querying Products Ordered in Each Order

To find out the products ordered in each order, including their quantity and unit price, use the following query:

MATCH (p:Product)-[:BELONGS_TO]->(o:Order)
RETURN id(o) as orderId, collect(p)

Querying Clients and Shipping Addresses

To determine which client made each order and where each order was shipped, use this query:

MATCH (c:Client)-[:MADE]->(o:Order)-[:SHIPPED_TO]->(adr:Address)
RETURN c.name as client, id(o) as orderId, adr.street, adr.city, adr.country

FAQ

What is Cypher Query Language (CQL)?

Cypher Query Language (CQL) is a powerful query language designed specifically for querying and updating graph databases. It allows you to interact with data in a way that emphasizes the relationships between data points.

How does CQL differ from SQL?

While SQL is designed for querying relational databases, CQL is designed for graph databases. This means that CQL excels at handling complex, highly connected data, whereas SQL is better suited for tabular data structures.

Can I use CQL with any database?

CQL is primarily used with Neo4j, a popular graph database management system. However, other graph databases may have their own query languages with similar capabilities.

What are the benefits of using CQL?

CQL allows for intuitive querying of graph databases, making it easier to manage and analyze data with complex relationships. It supports a rich set of commands for creating, updating, and deleting nodes and relationships, as well as powerful query capabilities.

Is CQL difficult to learn?

CQL is designed to be user-friendly and intuitive. If you are familiar with SQL, you will find many similarities in CQL. The main difference lies in how data relationships are handled.

How can I optimize my CQL queries?

Optimizing CQL queries involves understanding your graph's structure and using efficient query patterns. Indexing frequently searched properties and avoiding unnecessary full graph traversals can significantly improve performance.

Conclusion

Cypher Query Language (CQL) is a robust tool for managing graph databases, offering powerful capabilities for querying and updating complex, highly connected data. By mastering CQL, you can leverage the full potential of graph databases, making it easier to handle intricate data relationships and perform sophisticated analyses.

위 내용은 알아야 할 필수 JavaScript 인터뷰 질문의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.