Low-power wide-area (LPWA) communication has gained increasing attention in recent years with the rapid growth of fifth generation evolution (5G), the Internet of Things (IoT), and mobile computing. Narrowband Internet of Things (NB-IoT) is one kind of LPWA technology based on cellular IoT, which supports massive connections, wide area coverage, ultra-low power consumption, and ultra-low cost. Research on NB-IoT communication is increasingly attractive. Network calculus theory facilitates the performance analysis and network optimization of the NB-IoT system. We aim to analyze and optimize the NB-IoT networks. In this paper, we construct a random access traffic model including the NB-IoT user equipment (UE) arrival process and eNB service process. Then, we utilize the stochastic network calculus (SNC) to analyze the network delay in NB-IoT traffic model. Random latency bounds in different arrival processes are derived. Simulations show that SNC can evaluate the system delay under different distributions effectively. For the condition that numerous UEs access simultaneously following the Beta distribution, we first propose an improved K-means algorithm to cluster the NB-IoT terminals. Then, we raise the scheduling strategy on the basis of priority. It consists of the priority generation algorithm IPGNTQ and the NB-IoT task scheduling algorithm SANTQ. The extensive experiment results verify that our proposed optimized strategy can alleviate the network congestion effectually. Moreover, we compare our proposed optimized scheme with four existing uplink traffic scheduling schemes, showing that ours outperforms all of them.