<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: crazycs</title>
    <description>The latest articles on DEV Community by crazycs (@crazycs520).</description>
    <link>https://dev.to/crazycs520</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F502110%2Fd2cdabbc-abe1-4356-9220-b0debd6a7bf1.png</url>
      <title>DEV Community: crazycs</title>
      <link>https://dev.to/crazycs520</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/crazycs520"/>
    <language>en</language>
    <item>
      <title>TiGraph: 8,700x Computing Performance Achieved by Combining Graphs + the RDBMS Syntax</title>
      <dc:creator>crazycs</dc:creator>
      <pubDate>Tue, 06 Apr 2021 04:13:57 +0000</pubDate>
      <link>https://dev.to/crazycs520/tigraph-8-700x-computing-performance-achieved-by-combining-graphs-the-rdbms-syntax-1e33</link>
      <guid>https://dev.to/crazycs520/tigraph-8-700x-computing-performance-achieved-by-combining-graphs-the-rdbms-syntax-1e33</guid>
      <description>&lt;p&gt;&lt;strong&gt;Authors:&lt;/strong&gt; &lt;a href="https://github.com/lonng"&gt;Heng Long&lt;/a&gt;, &lt;a href="https://github.com/crazycs520"&gt;Shuang Chen&lt;/a&gt;, &lt;a href="https://github.com/wjhuang2016"&gt;Wenjun Huang&lt;/a&gt; (Software Engineers at PingCAP)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transcreator:&lt;/strong&gt; &lt;a href="https://github.com/CaitinChen"&gt;Caitin Chen&lt;/a&gt;; &lt;strong&gt;Editor:&lt;/strong&gt; Tom Dewan&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XUGYEmPX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fnegu7rq1c15xgevt15.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XUGYEmPX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fnegu7rq1c15xgevt15.jpeg" alt="How do you store a graph in a relational database?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A graph database is a database that uses graph data structures to store and query data. &lt;a href="https://www.gartner.com/en/documents/3899263/an-introduction-to-graph-data-stores-and-applicable-use-"&gt;Gartner&lt;/a&gt; believes that graph data stores can efficiently model, explore, and query data with complex interrelationships across data silos. Graph analytics will grow in the next few years. They also think &lt;strong&gt;it's impossible to use SQL queries to analyze graph data in relational database management systems (RDBMSs)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But today, we want to say &lt;strong&gt;No&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Our &lt;a href="https://github.com/tigraph"&gt;TiGraph&lt;/a&gt; project implemented a new set of key-value encoding formats to add a graph mode to &lt;a href="https://docs.pingcap.com/tidb/stable"&gt;TiDB&lt;/a&gt;, a relational, distributed SQL database. &lt;strong&gt;TiGraph can analyze graphical data that is difficult for relational databases to process,&lt;/strong&gt; and &lt;strong&gt;it improves TiDB's computing performance by 8,700+ times&lt;/strong&gt; in four &lt;a href="https://en.wikipedia.org/wiki/Six_degrees_of_separation"&gt;degrees of separation&lt;/a&gt;. At &lt;a href="https://pingcap.com/community/events/hackathon2020/"&gt;TiDB Hackathon 2020&lt;/a&gt;, our team won the second prize.&lt;/p&gt;

&lt;p&gt;In this post, we'll share TiGraph's architecture, its benchmarks, our project innovations, potential uses for TiGraph, and our future plans.&lt;/p&gt;

&lt;h2&gt;
  
  
  TiGraph's architecture
&lt;/h2&gt;

&lt;p&gt;Hackathon was short, so we didn't have time to develop a complete graph database. Instead, we tried to seamlessly integrate a graph mode in TiDB:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We extended a graph traversal syntax in SQL statements that a DBA can quickly learn.&lt;/li&gt;
&lt;li&gt;We enabled TiDB to manipulate graph data and relational data in the same transaction.&lt;/li&gt;
&lt;li&gt;We let table query statements include graph traversal as a subquery, and we let table queries be subqueries in graph traversal.&lt;/li&gt;
&lt;li&gt;We compared the performance of TiDB with and without TiGraph for different degrees of separation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TiGraph's technology stack is consistent with TiDB's from the upper layer to the lower layer. Its main work includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Writes&lt;/p&gt;

&lt;p&gt;We added graph schema types &lt;code&gt;TAG&lt;/code&gt; and &lt;code&gt;EDGE&lt;/code&gt; to metadata management, which represent a graph's vertex and edge, respectively. When data is written to the system, the system detects the written object's schema. If the schema is &lt;code&gt;TAG&lt;/code&gt; or &lt;code&gt;EDGE&lt;/code&gt;, data is encoded in the graph data's key-value format and is committed via the two-phase commit protocol. TiDB also uses this approach.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Reads&lt;/p&gt;

&lt;p&gt;We added two execution operators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GraphTagReader&lt;/code&gt;. It reads the graph data's vertex data. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Traverse&lt;/code&gt;. It traverses the graph based on specified edges.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Graph calculation contains three parts: graph traversal, subgraph matching, and graph aggregation. Our Hackathon demo focused on graph traversal. Later, when we continue to develop this project, we'll design subgraph matching and graph aggregation operators.&lt;/p&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Impressive benchmark metrics
&lt;/h2&gt;

&lt;p&gt;Because time was limited at Hackathon, TiGraph only implemented the key-value logic in TiDB. In &lt;a href="https://docs.pingcap.com/tidb/stable/tikv-overview"&gt;TiKV&lt;/a&gt;, TiDB's distributed storage engine, we had no time to re-implement TiGraph in Rust. For testing we used Unistore, TiDB's built-in storage engine. &lt;/p&gt;

&lt;p&gt;Regarding the data size, at the beginning, we planned to generate 1 million vertices and 40 million edges. At four degrees of separation, we found that TiGraph could return a result while TiDB could not. This was because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We chose Unistore, which is for unit tests, instead of TiKV, which we use in production. &lt;/li&gt;
&lt;li&gt;In this scenario, there is no advantage to using a relational database, so TiDB couldn't return results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YozX6WFn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdejao2660fy4dtqxfs3.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YozX6WFn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdejao2660fy4dtqxfs3.jpeg" alt="TiGraph's benchmarks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Therefore, we tested a smaller amount of data: 100 thousand rows of data and 6.5 million edges. At this data scale, we compared performance between TiDB + Unistore and TiGraph in n-degree separation scenarios. The figure above shows that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TiGraph could run a six-degree separation test. In contrast, TiDB + Unistore could only run a three-degree separation test, and, after seven hours of processing, it still hadn't completed its four-degree separation test.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;As the degrees of separation increased, TiGraph's performance advantage significantly improved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In the two-degree separation test, TiGraph's performance was 190x as fast as that of TiDB + Unistore&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In the three-degree separation test, TiGraph's performance was 347x as fast as that of TiDB + Unistore.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;In the four-degree separation test, TiGraph finished the test in 3.05 s, while TiDB already used 26,637 s, and it needed more time to complete its test. So we can see that TiGraph's performance was at least 8,700x as fast as that of TiDB + Unistore.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Later, when TiGraph adopts TiKV, it will flexibly scale under the massive scale of graph data. In addition, when it combines with TiKV Coprocessor to push down graph data calculations, TiGraph's query performance will improve further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project difficulties: integrating a relational database with a graph database
&lt;/h2&gt;

&lt;p&gt;If an application uses a relational database and a graph database simultaneously, it's almost impossible to achieve transactions and strong consistency between the two databases. However, TiGraph can do it well. In this section, we'll explain how we made it possible by overcoming development difficulties.&lt;/p&gt;

&lt;p&gt;First, we designed a set of clauses that is highly extensible and highly compatible with the SQL syntax. The following examples show two popular graph query clauses, Gremlin and openCypher, for two degrees of separation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Gremlin
g.V().has("name","John").     -- Get the vertex with the name "John."
  out("knows").               -- Traverse the people that John knows (John'sfirst-degree connections). 
  out("knows").               -- Traverse the people that John's acquaintances know (John's second-degree connections).
  values("name")              -- Get these people's names.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- openCypher
MATCH (john {name: 'John'})-[:FRIEND]-&amp;gt;()-[:FRIEND]-&amp;gt;(fof)
RETURN john.name, fof.name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if we use two sets of query syntax in a single system, it would increase our users' learning costs. After some discussion, we finally determined our graph traversal clause like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM people WHERE name="John"
    TRAVERSE OUT(friend).OUT(friend).TAG(people);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This clause includes two parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The query result of the &lt;code&gt;SELECT… FROM …&lt;/code&gt; statement is the starting point for graph traversal (the &lt;code&gt;TRAVERSE&lt;/code&gt; clause).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;TRAVERSE&lt;/code&gt; clause specifies the &lt;code&gt;EDGE&lt;/code&gt; we want to traverse and uses graph traversal to query the starting point's second-degree connection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By contrast, if we used TiDB's existing SQL syntax without extending it, the statement would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT dst
     FROM follow
     WHERE src IN
         (SELECT dst
          FROM follow
          WHERE src IN
              (SELECT dst
               FROM follow
               WHERE src = 1234 )
            AND dst NOT IN
              (SELECT dst
               FROM follow
               WHERE src = 1234 )
            AND src != 1234
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparing the two examples above, we can see that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;TiGraph's syntax is SQL styled and very expressive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TiGraph introduces the &lt;code&gt;TRAVERSE&lt;/code&gt; clause to express graph traversal. This clause can seamlessly interact with TiDB relational queries' other clauses in combination. Therefore, &lt;strong&gt;we can reuse TiDB's existing execution operators and expressions, and the learning cost for users is very low&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;For example, we can reuse the &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;ORDER BY&lt;/code&gt;, and &lt;code&gt;LIMIT&lt;/code&gt; by adding filter conditions to the edge and input the result of graph traversal (the &lt;code&gt;TRAVERSE&lt;/code&gt; clause) to the &lt;code&gt;ORDER BY LIMIT&lt;/code&gt; clause:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM people
    WHERE name="John"               -- Transverse from the vertex with the name "John."
    TRAVERSE
        OUT(friend WHERE age&amp;gt;10).   -- The first degree of separation (age greater than 10).
        OUT(friend).                -- The second degree of separation.
        TAG(people)                 -- Output these friends' people property.
    ORDER BY name                   -- Sort the people of the second-degree separation by name.
    LIMIT 10;                       -- Take the top 10 of the people of the second-degree separation.
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Because TiDB operators rely on a strong schema design, to reuse these operators, TiGraph's &lt;code&gt;TAG&lt;/code&gt; and &lt;code&gt;EDGE&lt;/code&gt; must also have a strong schema. Therefore, the schema output by the graph calculation related operators can be highly compatible with relational operators' schema. TiDB's upper-layer operators don't need to know whether the bottom-layer is graph data or relational data. As long as the previous &lt;code&gt;TableScan&lt;/code&gt; operator is replaced with &lt;code&gt;GraphScan&lt;/code&gt; at the bottom layer, all capabilities can be reused for the upper layer.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TiGraph's three innovations
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://arxiv.org/abs/1905.12133"&gt;Researchers&lt;/a&gt; affiliated with Cornell University also tried to combine graphs and the RDBMS syntax at the SQL level. They tried to use SQL statements to combine &lt;code&gt;Stream&lt;/code&gt; and &lt;code&gt;Batch&lt;/code&gt;. However, no one in the academic community has combined graphs and the RDBMS syntax the way TiGraph does. The TiGraph project has achieved three innovations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Innovation #1: &lt;strong&gt;we designed a set of SQL-style graph traversal clauses&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Innovation #2: &lt;strong&gt;we can manipulate graph data and relational data in the same transaction while guaranteeing strong consistency&lt;/strong&gt;. Sometimes, to solve a problem, users must use both a graph database and a relational database. But it's almost impossible to achieve strong consistency between the two databases. Now, TiGraph has this ability. In the future, for subqueries, we only need to improve their performance and make them easier to use.&lt;/li&gt;
&lt;li&gt;Innovation #3: &lt;strong&gt;we implemented two different encoding models in TiKV&lt;/strong&gt;. One model encodes relational tables, and the other encodes graphs. To avoid conflicts caused by mixed storage in a single key-value engine, we add a &lt;em&gt;g&lt;/em&gt; prefix to isolate key-value engines at the base layer so they don't affect each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TiGraph's application scenarios
&lt;/h2&gt;

&lt;p&gt;TiGraph has many potential uses. For example, it may play an important role in financial anti-fraud, social networks, and knowledge graph scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Financial anti-fraud
&lt;/h3&gt;

&lt;p&gt;Through the user's relationship network to detect their association with the risk node, we can identify their risk degree, which can be a reference indicator. For example, it might be difficult to detect whether a user within three degrees of separation touches a risky node. It's hard to find the problem by looking at a single node and a single transaction. But TiGraph can detect and analyze correlation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can detect whether the user's multi-layer social relationship conforms to normal graph characteristics. If it's an isolated subgraph, it may be a fake relationship network, and the user is at high risk. For example, they may be on a block list or associated with a high-risk node.&lt;/li&gt;
&lt;li&gt;It can spot whether there are high-risk nodes in the multi-layer relationship network, such as risky nodes in the second degree of separation.&lt;/li&gt;
&lt;li&gt;It can use the Google Personal Rank and PageRank algorithms to calculate nodes' risk degrees in a relational network. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For organized and large-scale digital financial frauds, TiGraph could quickly analyze a criminal gang in a complex and help staff reach timely decisions about fraud blocking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Social networks
&lt;/h3&gt;

&lt;p&gt;LinkedIn includes first-degree, second-degree, and third-degree connections. It analyzes your social network relationships to help you expand your circle of connections.&lt;/p&gt;

&lt;p&gt;TiGraph's reach is even more comprehensive. It can calculate &lt;a href="https://en.wikipedia.org/wiki/Six_degrees_of_separation"&gt;degrees of separation&lt;/a&gt; in social networks. In addition, to obtain some in-depth information, you can combine social network data with your consumption records and other information. This helps the social platform's recommendation system increase &lt;a href="https://www.wordstream.com/conversion-rate"&gt;conversion rate&lt;/a&gt;. TiGraph can break data silos and establish a connection between isolated data. This results in a 1 + 1 &amp;gt; 2 effect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Knowledge graph
&lt;/h3&gt;

&lt;p&gt;In 2012, Google introduced the concept of the &lt;a href="https://en.wikipedia.org/wiki/Google_Knowledge_Graph"&gt;knowledge graph&lt;/a&gt;. Through certain methods, knowledge can be extracted and organized into a structure similar to a mind map, and then it can be queried in a graph database. The search engine can only tell users which pages the query results are related to, and users need to find answers on the pages themselves. But the knowledge graph can directly tell users the answers.&lt;/p&gt;

&lt;p&gt;For example, TiGraph can directly tell you, in Game of Thrones, who Elia Targaryen's husband's brothers and sisters are. Isn't that cool?&lt;/p&gt;

&lt;h2&gt;
  
  
  Our future plans with TiGraph
&lt;/h2&gt;

&lt;p&gt;In the future, we want to write a paper about TiGraph's implementation, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How we integrated the graph mode in the existing relational database (TiDB).&lt;/li&gt;
&lt;li&gt;TiGraph's syntax. We'll implement graph calculation's three operators.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll also continue to develop and implement the TiGraph project. Our main tasks are to implement key-value encoding in TiKV and implement graph calculation pushdown in the TiKV Coprocessor. Therefore, graph queries can directly reuse TiDB's execution operators and expressions, and we can seamlessly combine graph queries and relational queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  The team behind TiGraph
&lt;/h2&gt;

&lt;p&gt;The three hackers on the TiGraph team are all &lt;a href="https://github.com/pingcap/tidb/graphs/contributors"&gt;top developers in the TiDB community&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/lonng"&gt;Heng Long&lt;/a&gt; is the TiGraph team leader.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/crazycs520"&gt;Shuang Chen&lt;/a&gt; ranks in the top 5 on the TiDB Contributors list.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/wjhuang2016"&gt;Wenjun Huang&lt;/a&gt; is an experienced developer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any questions or want more details about TiGraph, join the &lt;a href="https://slack.tidb.io/invite?team=tidb-community&amp;amp;channel=everyone&amp;amp;ref=pingcap-blog"&gt;TiDB community on Slack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;At TiDB Hackathon 2020, many excellent, interesting projects were born. We'll be telling you about them in future &lt;a href="https://pingcap.com/blog/tag/Hackathon"&gt;blog posts&lt;/a&gt;. Stay tuned.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Metrics Relation Graph Helps DBAs Quickly Locate Performance Problems in TiDB</title>
      <dc:creator>crazycs</dc:creator>
      <pubDate>Fri, 30 Oct 2020 06:53:32 +0000</pubDate>
      <link>https://dev.to/crazycs520/metrics-relation-graph-helps-dbas-quickly-locate-performance-problems-in-tidb-2j76</link>
      <guid>https://dev.to/crazycs520/metrics-relation-graph-helps-dbas-quickly-locate-performance-problems-in-tidb-2j76</guid>
      <description>&lt;p&gt;&lt;a href="https://docs.pingcap.com/tidb/stable"&gt;TiDB&lt;/a&gt;, an open-source, distributed SQL database, provides detailed monitoring metrics through Prometheus and Grafana. These metrics are often the key to troubleshooting performance problems in the cluster.&lt;/p&gt;

&lt;p&gt;However, for novice TiDB users, understanding hundreds of monitoring metrics can be overwhelming. You may wonder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do these hundreds of metrics relate to each other?&lt;/li&gt;
&lt;li&gt;How can I quickly find which operations are the slowest?&lt;/li&gt;
&lt;li&gt;When I discover a slow write, how can I locate the cause?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TiDB 4.0.7 introduces a new feature in its web UI &lt;a href="https://docs.pingcap.com/tidb/stable/dashboard-intro"&gt;TiDB Dashboard&lt;/a&gt;: the &lt;a href="https://docs.pingcap.com/tidb/stable/dashboard-metrics-relation"&gt;metrics relation graph&lt;/a&gt;. It provides a tree diagram of the TiDB cluster performance metrics, enabling users to quickly see the relationships between TiDB internal processes and to get a new perspective on the cluster status.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;The metrics relation graph presents database metrics as parent-child relationships. In the graph, each box represents a monitoring item, and it includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The name of the item&lt;/li&gt;
&lt;li&gt;The total duration of the item&lt;/li&gt;
&lt;li&gt;The percentage the item duration takes up in the whole query duration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In each parent box, the total duration = its own duration + its child box’s duration. Take the following box as an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5KBwTzao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5iz0glmqntehayykle4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5KBwTzao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5iz0glmqntehayykle4m.png" alt="The metrics relation graph parent box"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A parent box&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tidb_execute&lt;/code&gt; item’s total duration is 19,306.46 s, accounting for 89.40% of the total query duration. Of this duration, the &lt;code&gt;tidb_execute&lt;/code&gt; item itself only consumes 9,070.18 s, and its child items consume the rest.&lt;/p&gt;

&lt;p&gt;If you hover the cursor over this box, you can see the detailed information about this monitoring item: its description, total count, average time, average P99 (99th percentile) duration, and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wRuIFJCm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uwzgqwmhx59t3cfdoqjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wRuIFJCm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uwzgqwmhx59t3cfdoqjn.png" alt="The metrics relation graph detailed information"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The size and color of each box is proportional to the percentage of the item’s duration in the total query duration. Therefore, the items that take up too much time clearly stand out in the diagram. You can easily focus on these items and follow the parent-child link to locate the root cause of the problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1 - investigating slow cluster response time
&lt;/h3&gt;

&lt;p&gt;Assume that your company just launched a new application. You notice that the cluster response gets much slower, even though the server CPU load is quite low. To find out the cause, you generate a metrics relation graph:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I7cKJ_Wy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a8y00dyniaebdnudxqwp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I7cKJ_Wy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a8y00dyniaebdnudxqwp.png" alt="tidb_query_total_time example 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the metrics relation graph, you get the following findings in a glance:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Box&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tidb_query.Update&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The &lt;code&gt;UPDATE&lt;/code&gt; statement takes up 99.59% of the total query duration.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tidb_execute&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The TiDB execution engine takes up 68.69% of the total duration.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tidb_txn_cmd.commit&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Committing the transaction takes up 30.66% of the total duration.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tidb_kv_backoff.txnLock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;When the transaction encounters lock conflict, the  backoff operation takes up 15%, which is much higher than the &lt;code&gt;tidb_kv_request&lt;/code&gt; that sends Prewrite and Commit RPC requests.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By now, you can say for sure that the &lt;code&gt;UPDATE&lt;/code&gt; statement has a severe write conflict. The next step is to find out &lt;a href="https://docs.pingcap.com/tidb/stable/troubleshoot-write-conflicts"&gt;which table and SQL statement causes the conflict&lt;/a&gt;, and then work with the application developers to avoid the write conflict.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2 - finding out why data import is slow
&lt;/h3&gt;

&lt;p&gt;Assume that you need to load a large batch of data into your TiDB cluster, but the import rate is slow. You want to know why, so again you generate a metrics relation graph:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8dBnG8Cg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zxzgzfr876es9brmm93u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8dBnG8Cg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zxzgzfr876es9brmm93u.png" alt="tidb_query_total_time example 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note the shaded box near the bottom of the tree: ‘tikv_raftstore_propose_wait’. This box indicates that the “propose” process of TiKV’s Raftstore has a long wait duration. This usually means that Raftstore has hit a bottleneck. Next, you can check the metrics of Raftstore CPU and the latency of the append log and apply log. If Raftstore’s thread CPU utilization is low, then the root cause may be in the disk. For more troubleshooting information, you can refer to &lt;a href="https://asktug.com/_/tidb-performance-map/#/tikv"&gt;TiKV Performance Map&lt;/a&gt; or &lt;a href="https://docs.pingcap.com/tidb/stable/troubleshoot-high-disk-io"&gt;Troubleshoot High Disk I/O Usage in TiDB&lt;/a&gt;. You may also check &lt;a href="https://docs.pingcap.com/tidb/stable/troubleshoot-hot-spot-issues"&gt;whether there’s a hotspot&lt;/a&gt; in the cluster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it out
&lt;/h2&gt;

&lt;p&gt;To generate a metrics relation graph, you need to deploy TiDB 4.0.7 (or later) and &lt;a href="https://prometheus.io/"&gt;Prometheus&lt;/a&gt;, an open-source monitoring system. We recommend you &lt;a href="https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup"&gt;deploy TiDB using TiUP&lt;/a&gt;, which automatically deploys Prometheus along with the cluster.&lt;/p&gt;

&lt;p&gt;Once you’ve deployed TiDB, you can login to &lt;a href="https://docs.pingcap.com/tidb/stable/dashboard-intro"&gt;TiDB Dashboard&lt;/a&gt; to view the overall status of the cluster. In the &lt;strong&gt;Cluster Diagnostics&lt;/strong&gt; page, configure the range start time and range duration, and click &lt;strong&gt;Generate Metrics Relation&lt;/strong&gt;. Your metrics relation graph is ready!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MdjuW-Yh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wljyv17e2su6q26mytm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MdjuW-Yh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wljyv17e2su6q26mytm5.png" alt="Generate a metrics relation graph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Our next step
&lt;/h2&gt;

&lt;p&gt;The metrics relation graph is aimed to help users quickly grasp the relationship between TiDB cluster load and numerous monitoring items.&lt;/p&gt;

&lt;p&gt;In the future, we plan to integrate this feature with the &lt;a href="https://asktug.com/_/tidb-performance-map/#/"&gt;TiDB Performance Map&lt;/a&gt; so that it can show the relationships between other associated monitoring items and even with their configurations. With this powerful feature, DBAs will be able to diagnose TiDB clusters with less effort and more efficiency.&lt;/p&gt;

&lt;p&gt;If you’re interested in the metics relation graph, feel free to visit &lt;a href="https://github.com/pingcap-incubator/tidb-dashboard"&gt;our repository&lt;/a&gt; to contribute to the code or raise your question.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>performance</category>
      <category>database</category>
    </item>
  </channel>
</rss>
